【发布时间】:2020-07-08 15:06:58
【问题描述】:
我有以下代码,使用 ef 3.1 核心值转换,我收到以下错误,知道吗?
实体类型“地址”需要定义主键。如果您打算使用无密钥实体类型调用“HasNoKey()”。
public class Person {
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
public IList<Address> Addresses { get; set; }
}
public class Address {
public string Type { get; set; }
public string Company { get; set; }
public string Number { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
public class PersonsConfiguration : IEntityTypeConfiguration<Person> {
public void Configure(EntityTypeBuilder<Person> builder) {
// This Converter will perform the conversion to and from Json to the desired type
builder.Property(e => e.Addresses).HasConversion(
v => JsonConvert.SerializeObject(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
v => JsonConvert.DeserializeObject<IList<Address>>(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
}
}
【问题讨论】:
标签: .net entity-framework entity-framework-core