How can I save a collection of CultureInfo type using EF Core without adding backing fields?

Joseph Gregory 20 Reputation points
2024-07-04T01:15:48.8333333+00:00

In a class, I have a collection of type List<CultureInfo> OfficialLanguages. I need to respect domain driven design for this project. I cannot add a backing field of type List<string> officialLanguagesCodes and handle it that way.

When I have a single property like User.Culture, I can handle a conversion like this in my OnModelCreating:

modelBuilder.Entity<User>().Property(u => u.Culture).HasConversion(c => c.Name, s => new(s));

How can I achieve something similar for a collection?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
753 questions
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,540 Reputation points Microsoft Vendor
    2024-07-04T08:11:17.3266667+00:00

    Hi,@Joseph Gregory. Welcome to Microsoft Q&A. 

    Use HasConversion to store List<CultureInfo> in the database as a string. You can use the following method.

    1.Install Newtonsoft.Json via Nuget

    2.Add code

    
    modelBuilder.Entity<User>().Property(e => e.OfficialLanguages).HasConversion(
    
        v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
    
        v => JsonSerializer.Deserialize<List<CultureInfo>>(v, (JsonSerializerOptions)null));
    
    

    If the above solutions do not solve your problem, please describe the database and model design more clearly, as well as your expected results. Update:

    According to my test, officialLanguage ​​can read it correctly even if you have a private read-only backing field.

    If you want officialLanguage to remain private and read-only and the table can correctly insert other fields, you could try adding a default value for officialLanguage.You could refer to the following code.

     

    
    entity.Property(e => e.OfficialLanguages).HasConversion(
    
        v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
    
        v => JsonSerializer.Deserialize<List<CultureInfo>>(v, (JsonSerializerOptions)null))
    
        .HasDefaultValue(new List<CultureInfo>() { });
    
    
    

    The officialLanguage is set to private read-only. You could only add values ​​to it through the database. You could print out the value to be added as follows and then manually copy it into the database.

    
    //Install Newtonsoft.Json via NuGet
    
    Console.WriteLine(JsonConvert.SerializeObject(new List<CultureInfo>() {
    
        new CultureInfo() { Id = 1, Name = "AA" },
    
        new CultureInfo() { Id = 2, Name = "BB" },
    
        new CultureInfo() { Id = 3, Name = "CC" }
    
        }));
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.