Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 288
Keith Barrows Avatar asked Feb 02 '10 23:02

Keith Barrows


People also ask

How do I update Entity Framework model from database first in .NET core?

Updating the Model If you need to re-scaffold the model after database schema changes have been made, you can do so by specifying the -f or --force option e.g.: dotnet ef dbcontext scaffold "Server=. \;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft. EntityFrameworkCore.

How do I update EDMX after database change?

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.

How do I regenerate EDMX?

There is no automatically refresh the EDMX (it will be nice from MS if they implement that at some point) and the best and most accurate way to refresh the EDMX is by deleting all tables in the Diagram and then deleting all complex types etc. in the Model Browser.


1 Answers

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).

like image 85
Simon P Stevens Avatar answered Sep 21 '22 14:09

Simon P Stevens