15

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

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

4

1 回答 1

19

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

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

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

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

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

于 2010-02-03T00:21:39.743 回答