开发者

Entity Framework 4.1 Code First: Single column foreign-key to multiple entities

开发者 https://www.devze.com 2023-04-01 10:09 出处:网络
I\'ve been trying to create model in EF 4.1 to represent a database schema with a single table and column holding foreign keys from two other tables, but have had little luck with both annotations and

I've been trying to create model in EF 4.1 to represent a database schema with a single table and column holding foreign keys from two other tables, but have had little luck with both annotations and the fluent API. A sample model is shown here:

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; }
}
开发者_如何学编程

Currently the configuration for these entities looks something like this for both User and Account modelBuilders:

this.HasOptional(u => u.ExtendedAttributes).WithRequired();

Any thoughts on how to do achieve? Many thanks.


It is even not possible with the database itself and EF will not put any abstraction for that. You must have separate column and navigation property for each entity.

0

精彩评论

暂无评论...
验证码 换一张
取 消