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?
Entity Framework does not presently support mapping a Dictionary natively.
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…
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.
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.
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/
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) ); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With