Home > Net >  How can i store list of values entity framework with mysql
How can i store list of values entity framework with mysql

Time:01-09

I want to store list of Ids of workers in my Project entity.

public class Project
{
    [Required] [Key] public Guid id { set; get; }
    ...Other fields
    [AllowNull] public List<Guid> workers { set; get; }
}

But MySql doesn't support array type and when i'm configuring model

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Project>(x =>
        {
            x.HasKey(y => y.id);
            x.Property(y => y.workers);
        });
    }

i have an error The property 'Project.workers' is of type 'List<Guid>' which is not supported by the current database provider. So, what is the best way so store array type with EF?

CodePudding user response:

you can use Conversion to combine list to string and deserialize It. like below;

modelBuilder.Entity<Project>(x =>
        {
            x.HasKey(y => y.id);
            x.Property(y => y.workers).HasConversion(x => JsonConvert.SerializeObject(x), // to converter
                                   x => JsonConvert.DeserializeObject<List<Guid>>(x));
        });
  • Related