3

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.

Brent
  • 411
  • 4
  • 11

1 Answers1

0

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for the response Ladislav. Though I was able to create a database with the foreign key relationships set up as I described (which is weird), you're absolutely correct in that I'm unable to insert actual data as such. So I suppose the option is then to map the keys to multiple columns, or create a one-to-one intermediary to do the mapping. Suggestions on the preferred method in this case? – Brent Aug 30 '11 at 17:16