Reputation: 4264
Is there any way to map a dictionary using Entity Framework 5?
From what I found while searching, it has not been supported until EF 4.1, not sure if EF 5 provides any innovative methods to map it easily.
I am using the dictionary to store a "variable number of properties" for each object of a class.
If it's not supported what would be the best alternative data structure to use in this scenario (supported by EF)?
Upvotes: 4
Views: 5698
Reputation: 364279
No dictionary is still not supported. You must use traditional one-to-many relation. Where each your current key/value pair in dictionary will be stored as record in table and related with foreign key to principal entity (that one which contained the dictionary). The entity should look like:
public class Item {
public int Id { get; set; } // unique autogenerated database key
public string Key { get; set; }
public string Value { get; set; }
public int PrincipalId { get; set; } // FK to principal entity
}
Types of properties and name of foreign key will differ for your current situation. The principal entity will just have:
public virtual ICollection<Item> Items { get; set; }
Upvotes: 6