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

时间:2021-05-14 02:08:47

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:

我的数据库中有一个名为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!

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

1 个解决方案

#1


18  

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. 数据库/存储方案(SSDL)

  3. The conceptual model (CSDL)
  4. 概念模型(CSDL)

  5. The mapping between conceptual and storage (MSL)
  6. 概念和存储(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是设计器并检查字段上的属性。 (完全有可能将可空的数据库字段映射到不可为空的概念字段,但显然在这种情况下不是您想要的)。

#1


18  

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. 数据库/存储方案(SSDL)

  3. The conceptual model (CSDL)
  4. 概念模型(CSDL)

  5. The mapping between conceptual and storage (MSL)
  6. 概念和存储(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是设计器并检查字段上的属性。 (完全有可能将可空的数据库字段映射到不可为空的概念字段,但显然在这种情况下不是您想要的)。