Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map a Dictionary in Entity Framework Code First Approach

Tags:

I have a dictionary like this:

/// <summary> /// Gets the leave entitlement details. /// </summary> /// <value>The leave entitlement details.</value> public Dictionary<string, EmployeeLeaveEntitlement> LeaveEntitlementDetails { get; set; }   

And I want to map it to the database. Is it possible to use a protected or private List<> for that? such as:

/// <summary> /// Gets the leave entitlement details. /// </summary> /// <value>The leave entitlement details.</value> public Dictionary<string, EmployeeLeaveEntitlement> LeaveEntitlementDetails { get; set; }   public List<EmployeeLeaveEntitlement> LeveEntitlementStore {     get     {         List<EmployeeLeaveEntitlement> leaveEntitlements = new List<EmployeeLeaveEntitlement>();          foreach (KeyValuePair<string, EmployeeLeaveEntitlement> leaveType in LeaveEntitlementDetails)         {             leaveEntitlements.Add(leaveType.Value);         }          return leaveEntitlements;     }     set     {         foreach (EmployeeLeaveEntitlement item in value)         {             this.LeaveEntitlementDetails.Add(item.LeaveType, item);         }     } } 

Can anyone help me?

like image 239
iJay Avatar asked Mar 08 '12 05:03

iJay


People also ask

Does Entity Framework support Dictionary?

Entity Framework does not presently support mapping a Dictionary natively.

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

Is C# Dictionary a map?

The Dictionary is an essential component of C# programming and is simply a collection of Maps. Also remember - there is no built-in map in the C# language.

What is mapping in Entity Framework?

It is a tool to access the database. More accurately, it's classified as an Object/Relational Mapper (ORM) which means it maps data in a relational database into objects of our applications. Entity Framework. It is a tool to access the database.


2 Answers

Entity Framework does not presently support mapping a Dictionary natively.

See the following for more information and work-arounds:

Entity Framework 4 POCO with Dictionary

EF Code First - Map Dictionary or custom type as an nvarchar

http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/a51ba903-2b8b-448e-8677-d140a0b43e89/

like image 53
Eric J. Avatar answered Oct 30 '22 08:10

Eric J.


EF Core 2.1 introduced a new feature called value conversion:

Value converters allow property values to be converted when reading from or writing to the database.

This feature highly simplifies the serialization approach mentioned in previous answers, which means, the introduction of on an additional "helper" property and the marking of your dictionary property as [NotMapped] becomes unnecessary.

Here are some lines of code tailored to your case (note, I am using Json.NET, but feel free to use your serializer of choice):

using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Newtonsoft.Json; using Newtonsoft.Json.Serialization;  namespace My.Name.Space {     public class MyEntity     {         public int Id { get; set; }         public Dictionary<string, EmployeeLeaveEntitlement> LeaveEntitlementDetails { get; set; }      }      public class MyEntityConfiguration : IEntityTypeConfiguration<MyEntity>     {         public void Configure(EntityTypeBuilder<MyEntity> builder)         {             builder.ToTable("MyEntity");             builder.HasKey(e => e.Id);              builder             .Property(e => e.LeaveEntitlementDetails)             .IsRequired()             .HasConversion(                 v => JsonConvert.SerializeObject(v),                 v => v == null                     ? new Dictionary<string, EmployeeLeaveEntitlement>() // fallback                     : JsonConvert.DeserializeObject<Dictionary<string, EmployeeLeaveEntitlement>>(v)             );         }     } } 
like image 45
B12Toaster Avatar answered Oct 30 '22 06:10

B12Toaster