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

发布于 2024-08-20 05:22:53 字数 2071 浏览 1 评论 0原文

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

您会注意到它有一个属性:

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

我还包含了下一个属性,您会注意到它被正确标记为:

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

什么给出?如何在数据库架构中进行简单的更改,并真正根据这些更改更新实体框架?!每次发生变化时,我都必须删除并重新创建模型!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2024-08-27 05:22:53

请注意,此答案使用 EDMX 映射文件处理原始实体框架 v1。这不再适用于现代 EF。如果有人仍然支持遗留应用程序,请在下面留下完整的原始答案。


实体框架使用 XML 文件 (edmx) 来指定数据库方案和映射。当您单击“从数据库更新模型”时,将更新此 edmx 文件。

接下来,当您编译应用程序时,会解析此 edmx 文件并生成您正在查看的支持类,因此如果您想查看支持类中反映的更改,您需要更新模型,然后重新编译。

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

  1. 数据库/存储方案 (SSDL)
  2. 概念模型 (CSDL)
  3. 概念和存储之间的映射 (MSL)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文