EF 5 Code First Dictionary Mapping

Is there a way to map a dictionary using Entity Framework 5?

From what I found during the search, it was not supported until EF 4.1, not sure if EF 5 provides any innovative methods for easily matching it.

I use a dictionary to store a "variable number of properties" for each class object.

If it does not support what would be the best alternative data structure to be used in this scenario (supported by EF)?

+4
source share
1 answer

The dictionary is still not supported. You should use the traditional one-to-many relationship. Where each of your current key / value pairs in the dictionary will be stored as an entry in the table and associated with a foreign key with the main object (which contains the dictionary). The entity should look like this:

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 } 

The property types and foreign key name will be different for your current situation. The main object will have:

 public virtual ICollection<Item> Items { get; set; } 
+6
source

Source: https://habr.com/ru/post/1436873/


All Articles