EF 代码生成模板:找出不同之处?

EF code generation template: spot the difference?

我前段时间使用旧的 ObjectContext 模板创建了一个 EF 项目(首先是 DB)。这会将所有内容生成一个巨大的 honkin' 文件。这有一些缺点,比如如果我想查看文件,IDE 需要一段时间才能将其全部加载到内存中,修改需要一段时间才能刷新。此外,它使源代码控制历史中的事情很难一目了然:如果我想知道发生了什么变化,我必须在一个巨大文件的两个版本之间进行文件差异,而不是简单地查看孤立文件中的变化,生成为每个 class.

一个

所以我决定尝试创建一个代码模板,该模板将生成 相同的 代码,每个 class 只有一个文件。我认为我做得很好,因为代码编译得很好......但是当我打开应用程序时,我得到一个错误:

Schema specified is not valid. Errors:

The property for the relationship 'AssessmentSchool' contains a Role 'Assessment' has a type 'SchoolManagement.BL.Assessment' that is not valid for a relationship End. Change the End Role to an EntityType.

The property for the relationship 'AssessmentSchool' contains a Role 'School' has a type 'SchoolManagement.BL.School' that is not valid for a relationship End. Change the End Role to an EntityType.

然后它继续列出系统中的每个多对多交集 table(两次;关系的每一端一个)。在 AssessmentSchool 的情况下,table 只是有一个 AssessmentID 和一个 SchoolID.

所以我比较了旧MyContext.designer.cs中关系的声明,找到任何提到关系的地方,并与新代码进行比较。你能看出区别吗?

旧数据上下文:

[assembly: EdmRelationshipAttribute("SchoolManagement.BL", "AssessmentSchool", "Assessment", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(SchoolManagement.BL.Assessment), "School", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(SchoolManagement.BL.School))]

新数据上下文:

[assembly: EdmRelationshipAttribute("SchoolManagement.BL", "AssessmentSchool", "Assessment", RelationshipMultiplicity.Many, typeof(SchoolManagement.BL.Assessment), "School", RelationshipMultiplicity.Many, typeof(SchoolManagement.BL.School), false)]

(是的,有一些小区别:新文件在文件顶部有一个 using System.Data.Metadata.Edm 语句,isForeignKey 参数的 false 无论如何都是默认值值。我尝试手动删除该参数,但没有任何区别。)

旧评估class:

[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("SchoolManagement.BL", "AssessmentSchool", "School")]
public EntityCollection<School> Schools
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<School>("SchoolManagement.BL.AssessmentSchool", "School");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<School>("SchoolManagement.BL.AssessmentSchool", "School", value);
        }
    }
}

新评估class:

    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("SchoolManagement.BL", "AssessmentSchool", "School")]
    public virtual EntityCollection<School> Schools
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<School>("SchoolManagement.BL.AssessmentSchool", "School");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<School>("SchoolManagement.BL.AssessmentSchool", "School", value);
            }
        }
    }

对称关系也在Classclass,这里就不一一列举了。

这段代码在我看来非常相似,编译也很好,但只是抛出了上面的运行时错误。显然我在某处遗漏了另一个技巧。但是什么?

发现问题:我生成的 EntityObject 类 没有装饰器:

[EdmEntityTypeAttribute(NamespaceName="SchoolManagement.BL", Name="Assessment")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Assessment : EntityObject
{
  ...
}