通过 Code First 方法使用 List<int> 属性 创建数据库
Creating A Database With List<int> Property Through Code First Approach
我正在尝试通过代码优先方法使用 MySQL 和我的实体创建一个数据库,但我在尝试时遇到了一些错误。该错误是:
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'.
所以,我猜我可能会遇到这样的错误,但老实说我不知道如何处理它。
我的方法:
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();
}
}
}
我的实体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; }
}
我的背景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);
}
}
如何将其添加为列表或 table 中的单个功能?
哈林,
我很确定您的问题与此线程完全相同:
这是我通过复制您的错误消息并将其粘贴到 Google 中找到的。
您的问题正是错误消息所说的。将 C# class 映射到数据库 table 时,无法映射 List<>
,因为 SQL 中没有 List<>
类型。意思是,没有 SQL 列可以具有 List<>
类型。 String、Int、DateTime……没问题。但是 SQL table 中的单个单元格并非旨在包含集合。它只能包含一个值。
您需要从 class 中删除此字段,或者用 [NotMapped]
修饰它,如下所示:
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; }
}
我正在尝试通过代码优先方法使用 MySQL 和我的实体创建一个数据库,但我在尝试时遇到了一些错误。该错误是:
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'.
所以,我猜我可能会遇到这样的错误,但老实说我不知道如何处理它。
我的方法:
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();
}
}
}
我的实体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; }
}
我的背景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);
}
}
如何将其添加为列表或 table 中的单个功能?
哈林,
我很确定您的问题与此线程完全相同:
这是我通过复制您的错误消息并将其粘贴到 Google 中找到的。
您的问题正是错误消息所说的。将 C# class 映射到数据库 table 时,无法映射 List<>
,因为 SQL 中没有 List<>
类型。意思是,没有 SQL 列可以具有 List<>
类型。 String、Int、DateTime……没问题。但是 SQL table 中的单个单元格并非旨在包含集合。它只能包含一个值。
您需要从 class 中删除此字段,或者用 [NotMapped]
修饰它,如下所示:
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; }
}