实体框架 - 更新型号从数据库... - 没有更新发生!实体、框架、型号、发生

2023-09-03 10:53:20 作者:戒不掉的心动

我在我的数据库的表中调用 CompanyDetails 。它有一个名为列 VARCHAR或CharacterId(255)。我只是改变了它从一个 NOT NULL 列到 NULL 列。我跑了更新型号从数据库...在模型浏览器的命令,以及在EDMX文件查看器。这是它在设计器创建的:

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()]

怎么办?我怎样才能让我的DB模式简单的改变和真正的实体框架基于这些变化更新?我不得不删除并重新创建模型中的每个,每次有一个变化!

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!

推荐答案

实体框架使用的XML文件(EDMX)指定数据库模式和映射。当您单击从数据库更新模式,它是更新了此EDMX文件。

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.

接下来,当您编译应用程序,此EDMX文件进行分析,并生成你正在寻找的支持类,因此,如果你想看到的变化反映在您需要更新模型的支持类,然后重新编译

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.

最后,你还必须记住,EDMX包含3个事情。

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

在数据库/存储方案(SSDL) 在概念模型(CSDL) 在概念上和存储(MSL)之间的映射

更新数据库,并单击更新将更新SSDL,但不一定会自动对概念模型进行必要的更改,则可能需要打开EDMX是设计师和检查领域的性能。 (这是完全可能的映射到一个非空的概念场可为空的数据库字段,但很明显,在你想要什么这种情况下,是不是)。

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