实体框架 4.1 中带有外键的抽象属性?

发布于 2025-01-01 08:26:01 字数 1850 浏览 5 评论 0原文

更新: 我更新了类,使其看起来更像我的实际模型。当我从文档基础实体中删除可分配属性时,一切正常。

这样的东西可以在 EF 4.1 中映射吗?

public abstract class Entity
{
   public Guid Id {get;set;}
}    

public abstract class Assignable:Entity
    {

    }

    public class Contact: Assignable
    {
         public string Name {get;set;}
    }

    public class Partner: Assignable
    {
         public string Area {get;set;}
    }

    public abstract class Document: Entity        {

         public Guid AssignedToId {get;set}
         public Assignable AssignedTo {get;set;
    }

    public class Submittal: Document
    {
         public string SomeProperty {get;set;}
    }

映射:

    public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
        where TEntity : Entity
        {
            protected EntityConfiguration()
            {
                HasKey(e => e.Id);
                Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            }
        }

    public class AssignableEntityMapping<TEntity>: EntityConfiguration<TEntity> where TEntity: Assignable
{

}
    public class DocumentEntityMapping<TEntity>: EntityConfiguration<TEntity>
        {
            public DocumentEntityMapping()
            {

                HasOptional(e => e.AssignedTo).WithMany().HasForeignKey(e => e.AssignedToId);
            } 
        }

public class ContactMapping: AssignableEntityMapping<Contact>
{
....
}

public class PartnerMapping: AssignableEntityMapping<Partner>
{
....
}

public class SubmittalMapping: DocumentEntityMapping<Submittal>
{
....
}

如果可能的话,如何设置映射以使AssignedToId 成为具体实体之一的外键?我为 Document 实体创建了一个映射,但收到错误:“属性 'Id' 不是类型上声明的属性...”我猜测这是因为 EF 不知道如何区分 AssignedToId 所指的内容?

UPDATE:
I updated the classes to look more like my actual model. When I remove the Assignable property from the Document base entity, everything works.

Is something like this possible to map in EF 4.1?

public abstract class Entity
{
   public Guid Id {get;set;}
}    

public abstract class Assignable:Entity
    {

    }

    public class Contact: Assignable
    {
         public string Name {get;set;}
    }

    public class Partner: Assignable
    {
         public string Area {get;set;}
    }

    public abstract class Document: Entity        {

         public Guid AssignedToId {get;set}
         public Assignable AssignedTo {get;set;
    }

    public class Submittal: Document
    {
         public string SomeProperty {get;set;}
    }

Mapping:

    public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
        where TEntity : Entity
        {
            protected EntityConfiguration()
            {
                HasKey(e => e.Id);
                Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            }
        }

    public class AssignableEntityMapping<TEntity>: EntityConfiguration<TEntity> where TEntity: Assignable
{

}
    public class DocumentEntityMapping<TEntity>: EntityConfiguration<TEntity>
        {
            public DocumentEntityMapping()
            {

                HasOptional(e => e.AssignedTo).WithMany().HasForeignKey(e => e.AssignedToId);
            } 
        }

public class ContactMapping: AssignableEntityMapping<Contact>
{
....
}

public class PartnerMapping: AssignableEntityMapping<Partner>
{
....
}

public class SubmittalMapping: DocumentEntityMapping<Submittal>
{
....
}

If this is possible how do I setup the mapping so that the AssignedToId is a foreign key to one of the concrete entities? I created a mapping for the Document entity and I get the error: "The property 'Id' is not a declared property on type..." I am guessing it is because EF doesn't know how to discriminate what the AssignedToId refers to?

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

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

发布评论

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

评论(1

无远思近则忧 2025-01-08 08:26:02

我在 ContactPartner 实体周围有很多额外的架构,我无法使用诸如 TPH(每个层次结构表) 方法。 EF 不知道如何映射 Document 类中的AssignedToId,因为我尝试使用同时列出每个具体类型 (TPC) 的表。我现在只是将所有“可分配”类型添加到 Document 类中。如果还有其他方法可以解决这个问题,我想知道。

public abstract class Document: Entity
{
     public Guid? AssignedContactId {get;set;}
     public Contact AssignedContact {get;set;}
     public Guid? AssignedPartnerId {get;set;}
     public Partner AssignedPartner {get;set;}
}

I had a lot of extra architecture around the Contact and Partner entities that I could not use something like a TPH (Table Per Hierarchy) approach. EF doesn't know how to map the AssignedToId in the Document class since i was trying to use Table per Concrete Type (TPC) at the same time. I ended up just adding all the "assignable" types to the Document class for now. If there is another way around this, I'd like to know.

public abstract class Document: Entity
{
     public Guid? AssignedContactId {get;set;}
     public Contact AssignedContact {get;set;}
     public Guid? AssignedPartnerId {get;set;}
     public Partner AssignedPartner {get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文