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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:51:45  来源:igfitidea点击:

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

.netentity-frameworkupdatemodel

提问by Keith Barrows

I have a table in my DB called CompanyDetails. It has a column called CharacterID varchar(255). I just changed it from a NOT NULLcolumn to a NULLcolumn. 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:

我的数据库中有一个名为CompanyDetails. 它有一个名为CharacterID varchar(255). 我只是将它从一NOT NULL列更改为一NULL列。我在模型浏览器和 EDMX 文件查看器中运行了“从数据库更新模型...”命令。这是它在设计器中创建的内容:

/// <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!

是什么赋予了?如何在我的数据库架构中进行简单的更改并真正根据这些更改更新实体框架?!每次发生变化时,我都不得不放弃并重新创建模型!

回答by Simon P Stevens

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.

实体框架使用 XML 文件(edmx)来指定数据库方案和映射。当您单击“从数据库更新模型”时,更新的是这个 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 文件并生成您正在查看的支持类,因此如果您想查看支持类中反映的更改,您需要更新模型,然后重新编译。

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

最后,您还必须记住 edmx 包含 3 项内容。

  1. The database/storage scheme (SSDL)
  2. The conceptual model (CSDL)
  3. The mapping between conceptual and storage (MSL)
  1. 数据库/存储方案 (SSDL)
  2. 概念模型 (CSDL)
  3. 概念和存储之间的映射 (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).

更新数据库并单击“更新”将更新 SSDL,但不一定会自动对概念模型进行所需的更改,您可能需要打开 edmx 设计器并检查字段上的属性。(完全有可能将可为空的数据库字段映射到不可为空的概念字段,但显然在这种情况下这不是您想要的)。