Search code examples
c#databaseentity-frameworkasp.net-coreef-code-first

Creating A Database With List<int> Property Through Code First Approach


I'm trying to create a database using MySQL with my entities by a code first approach, but I got some errors when I tried. That error was:

InvalidOperationException: The property 'SymptomousInBodySublocations.HealthSymptomLocationIDs' could not be mapped, because it is of type 'List' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

So, I guessed that I could get an error like this, but honestly I don't any idea about how to handle it.

My Method:

public static void InsertData()
{
    List<SymptomousInBodySublocations> SymtomsInBodySublocationsList = MedicService.SymptomsInBodySublocations();

    var semptominbodylocation = new SymptomousInBodySublocations();

    var context = new SymptomsRepo();

    using (context)
    {
        context.Database.EnsureCreated();
        foreach(SymptomousInBodySublocations symptomousInBodySublocations in SymtomsInBodySublocationsList)
        {
            semptominbodylocation.ID = symptomousInBodySublocations.ID;
            semptominbodylocation.Name = symptomousInBodySublocations.Name;
            semptominbodylocation.HasRedFlag = symptomousInBodySublocations.HasRedFlag;
            semptominbodylocation.HealthSymptomLocationIDs = symptomousInBodySublocations.HealthSymptomLocationIDs;
            semptominbodylocation.ProfName = symptomousInBodySublocations.ProfName;
            semptominbodylocation.Synonyms = symptomousInBodySublocations.Synonyms;
            context.SymptomousInBodySublocations.Add(semptominbodylocation);
            context.SaveChanges();
        }
    }
}

My Entity Class:

using System.Collections.Generic;

public class SymptomousInBodySublocations
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool HasRedFlag { get; set; }
    public List<int> HealthSymptomLocationIDs { get; set; }
    public string ProfName { get; set; }
    public List<string> Synonyms { get; set; }
}

My Context Class:

using Microsoft.EntityFrameworkCore;

public class SymptomsRepo : DbContext
{
    public SymptomsRepo(){  }

    public SymptomsRepo(DbContextOptions<SymptomsRepo> options) :base(options){}
    public DbSet<SymptomousInBodySublocations> SymptomousInBodySublocations {get; set;}

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseMySQL("server=localhost;database=Symptoms;user=root;password=password");
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
    }
}

How can I add it as a list or as a single feature inside a table?


Solution

  • Halin,

    I'm pretty sure your question is an exact duplicate of this thread:

    The property 'PropertyName' could not be mapped, because it is of type 'List<decimal>'

    Which I found by copying your error message and pasting it into Google.

    Your problem is exactly what the error message says. When mapping a C# class to a database table, you can not map a List<> because there is no List<> type in SQL. Meaning, no SQL column can have a type of a List<>. String, Int, DateTime... no problem. But a single cell in a SQL table is not designed to contain a collection. It can only contain a single value.

    You either need to remove this field from your class, or decorate it with [NotMapped] as follows:

    public class SymptomousInBodySublocations
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool HasRedFlag { get; set; }
        [NotMapped]
        public List<int> HealthSymptomLocationIDs { get; set; }
        public string ProfName { get; set; }
        [NotMapped]
        public List<string> Synonyms { get; set; }
    }