实体框架 4.1 中带有外键的抽象属性?
更新: 我更新了类,使其看起来更像我的实际模型。当我从文档基础实体中删除可分配属性时,一切正常。
这样的东西可以在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在
Contact
和Partner
实体周围有很多额外的架构,我无法使用诸如 TPH(每个层次结构表) 方法。 EF 不知道如何映射Document
类中的AssignedToId,因为我尝试使用同时列出每个具体类型 (TPC) 的表。我现在只是将所有“可分配”类型添加到 Document 类中。如果还有其他方法可以解决这个问题,我想知道。I had a lot of extra architecture around the
Contact
andPartner
entities that I could not use something like a TPH (Table Per Hierarchy) approach. EF doesn't know how to map the AssignedToId in theDocument
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.