Entity Framework - Update Model From Database... - no update happens!
Asked Answered
P

1

16

I have a table in my DB called CompanyDetails. It has a column called CharacterID varchar(255). I just changed it from a NOT NULL column to a NULL column. I ran the 'Update Model From Database...' command in the model browser as well as in the EDMX file viewer. This is what it created in the designer:

/// <summary>
/// There are no comments for Property CharacterId in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public string CharacterId
{
    get
    {
        return this._CharacterId;
    }
    set
    {
        this.OnCharacterIdChanging(value);
        this.ReportPropertyChanging("CharacterId");
        this._CharacterId = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
        this.ReportPropertyChanged("CharacterId");
        this.OnCharacterIdChanged();
    }
}
private string _CharacterId;
partial void OnCharacterIdChanging(string value);
partial void OnCharacterIdChanged();
/// <summary>
/// There are no comments for Property URLDomain in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public string URLDomain
{
    get
    {
        return this._URLDomain;
    }
    set
    {
        this.OnURLDomainChanging(value);
        this.ReportPropertyChanging("URLDomain");
        this._URLDomain = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
        this.ReportPropertyChanged("URLDomain");
        this.OnURLDomainChanged();
    }
}
private string _URLDomain;
partial void OnURLDomainChanging(string value);
partial void OnURLDomainChanged();

You will notice that it has an attribute of:

[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]

I also included the next property and you will notice that it is correctly marked as:

[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]

What gives? How can I make simple changes in my DB schema and really get the Entity Framework to Update based on those changes?! I've had to drop and recreate the model each and everytime there was a change!

Pr answered 2/2, 2010 at 23:44 Comment(0)
P
20

Please be aware this answer is dealing with the original Entity Framework v1 using an EDMX mapping file. This no longer applies to modern EF. Leaving full original answer below in case some people are still supporting legacy applications.


The entity framework uses an XML file (the edmx) to specify the database scheme and mapping. When you click "update model from database" it is this edmx file that is updated.

Next, when you compile your app, this edmx file is parsed and the backing classes you are looking at are generated, so if you want to see the change reflected in the backing classes you need to update the model, and then recompile.

Finally, you also have to remember that the edmx contains 3 things.

  1. The database/storage scheme (SSDL)
  2. The conceptual model (CSDL)
  3. The mapping between conceptual and storage (MSL)

Updating the database and clicking "update" will update the SSDL but won't necessarily make the required changes automatically to the conceptual model, you may need to open up the edmx is the designer and check the properties on the field. (It is entirely possible to have a nullable database field mapped to a non-nullable conceptual field, but obviously in this case that isn't what you want).

Protanopia answered 3/2, 2010 at 0:21 Comment(8)
+1. This is right. If you've already generated a property in CSDL, you must update it manually. But the SSDL will always be regenerated from scratch. The EF presumes you want your CSDL customizations.Antecedent
Thanks Craig. As an extra note, I believe you can use the tool edmgen (msdn.microsoft.com/en-us/library/bb387165.aspx) with the switch /mode:FromSSDLGeneration or /mode:FullGeneration to force generation of the CSDL+MSL or all blocks, if that is what you need.Protanopia
Where do I find these 3 files and do I have to hand edit them each time I make changes like this? I have done a search on my hard drive and cannot find these files at all so am wondering if they are internally generated, and if so, how do I open them? (What a newb question!) :)Pr
As Simon stated, all three sections are part of your EDMX file. Right click your model in Solution Explore, choose Open With, then select the XML editor.Antecedent
@Keith. You can edit the edmx xml manually, (and sometimes I find it easier), but most stuff can be done through the GUI. You'll notice that you can set the nullable on each classes property in the main screen (select the field and hit F4 to being up the properties) , but if you look in the model window, you can also browse the database tables and expand each field and set nullable on each database fields too.Protanopia
Doesn't this seem crazy? It does to me. If I remove objects from the db (in my case I changed the names of stored procs), then do an Update, I expect them to disappear from the model AND from the TT files. Instead they persist, and code is generated for calling procs that no longer exist. In my case I was able to edit the XML manually, removing all the procs, then ran "custom tool" against the TT's to see the objects disappear, then opened the EDMX in the designer and did an update, importing the "new" (i.e. renamed) procs back in.Ballad
I have to ask. What is the point of the EDMX Designer? It seems like a dangerous tool to anyone without current backups. The problems that it causes really makes me question the wisdom of using EF at all.Grainger
@DougKimzey this answer is 13 years old and refers to the original EF v1. Modern EF is actually nothing like this and no longer uses an EDMX file to store mapping info. I totally agree, EDMX was a liability, I wouldn't advocate for using it now. I'll update the answer to indicate this.Protanopia

© 2022 - 2024 — McMap. All rights reserved.