我一直在尝试在 EF 4.1 中创建模型来表示具有单个表和列的数据库模式,其中包含来自其他两个表的外键,但在注释和流畅的 API 方面运气不佳。此处显示了示例模型:
public class User
{
...
public virtual ExtendedAttribute ExtendedAttributes { get; set; }
}
public class Account
{
...
public virtual ExtendedAttribute ExtendedAttributes { get; set; }
}
public class ExtendedAttribute
{
public Guid Id {get; set;}
public Guid ItemId {get; set;} // both Account.Id and User.Id stored here
public string Value { get; set; }
}
目前,对于用户和帐户模型构建器,这些实体的配置看起来像这样:
this.HasOptional(u => u.ExtendedAttributes).WithRequired();
关于如何实现的任何想法?非常感谢。
最佳答案
数据库本身甚至是不可能的,EF 不会为此添加任何抽象。每个实体必须有单独的列和导航属性。
关于entity-framework-4 - Entity Framework 4.1 代码优先 : Single column foreign-key to multiple entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7236013/