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

时间:2010-02-02 23:44:22

标签: .net entity-framework updatemodel

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

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

1 个答案:

答案 0 :(得分:18)

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

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

最后,您还必须记住edmx包含3件事。

  1. 数据库/存储方案(SSDL)
  2. 概念模型(CSDL)
  3. 概念和存储(MSL)之间的映射
  4. 更新数据库并单击“更新”将更新SSDL,但不一定会自动对概念模型进行必要的更改,您可能需要打开edmx是设计器并检查字段上的属性。 (完全可以将可空的数据库字段映射到不可为空的概念字段,但显然在这种情况下不是您想要的)。