C# 财产。。。无法映射,因为它的类型为';字符串[]和#x27;它不是受支持的基元类型或有效的实体类型
此代码是使用paste special for XML自动生成的类C# 财产。。。无法映射,因为它的类型为';字符串[]和#x27;它不是受支持的基元类型或有效的实体类型,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,此代码是使用paste special for XML自动生成的类 [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class PersonName { private str
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PersonName
{
private string firstNameField;
private string surnameField;
private string[] originalScriptNameField;
public string FirstName
{
get
{
return this.firstNameField;
}
set
{
this.firstNameField = value;
}
}
public string Surname
{
get
{
return this.surnameField;
}
set
{
this.surnameField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("OriginalScriptName")]
public string[] OriginalScriptName
{
get
{
return this.originalScriptNameField;
}
set
{
this.originalScriptNameField = value;
}
}
}
我在我的上下文类中使用实体框架(代码优先)
public DbSet<PersonName> PersonNames { get; set; }
public DbSet PersonNames{get;set;}
但是在运行时,我得到了这个错误
财产。。。无法映射,因为它的类型为“string[]”
它不是受支持的基元类型或有效的实体类型。任何一个
显式映射此属性,或使用“[NotMapped]”忽略它
属性或使用中的“EntityTypeBuilder.Ignore”
“关于模型创建”
简单地说,在最常见的名为array的数据库中没有字段类型 修正1 不要那样做。通常,您会将这些内容放在另一个表(相关实体)中 修复2(
ValueConverter
hackery)
值转换器允许在读取时转换属性值
从数据库或写入数据库
警告
使用值转换可能会影响EF Core
将表达式转换为SQL
示例
var converter = new ValueConverter<string[], string>(
x => string.Join(";",x),
x => x.Split(';', StringSplitOptions.RemoveEmptyEntries));
modelBuilder.Entity<PersonName>()
.Property(e => e.OriginalScriptName)
.HasConversion(converter);
var转换器=新值转换器(
x=>string.Join(“;”,x),
x=>x.Split(“;”,StringSplitOptions.RemoveEmptyEntries));
modelBuilder.Entity()
.Property(e=>e.OriginalScriptName)
.转换(转换器);
注意:显然,上述操作可能会以多种方式失败,您的任务是使其适合您的情况。您是否已经有了答案“要么显式映射此属性,要么使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.ignore”忽略它。”?我不太清楚你在问什么…问题是从
将XML粘贴为类中自动生成的类
生成了大量数组字段类型。@Steve yeah你会遇到问题,生成XML类不能很好地与实体框架配合使用,但是你可以创建自己的模型,并将序列化的XML映射到它们,或者一起按摩,这可能不是复杂类型的最佳选择