1 回答
TA贡献1797条经验 获得超6个赞
有这个我必须dateRequested
用[NotMapped]
属性标记。否则 EF 会抛出一个异常
列名称“ContentModel_dateRequested”无效。
工作解决方案:
public class PersonModel
{
[Key]
[JsonProperty("ix")]
[XmlElement("ix")]
public int Index { get; set; }
[XmlElement("content")]
public ContentModel ContentModel { get; set; }
}
[ComplexType]//added
[XmlRoot(ElementName = "content")]
public class ContentModel
{
[JsonProperty("name")]
[XmlElement("name")]
public string Name { get; set; }
[JsonProperty("visits")]
[XmlElement("visits", IsNullable = true)]
public int? Visits { get; set; }
public bool ShouldSerializeVisits() { return Visits != null; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[NotMapped]//added
[XmlElement("date")]
public string dateRequested
{
get { return Date.ToString("yyyy-MM-dd"); }
set { Date = DateTime.ParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); }
}
}
并在我的班级中添加了新方法ApplicationDbContext:
//added
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<PersonModel>(table =>
{
table.OwnsOne(
x => x.ContentModel,
content =>
{
content.Property(x => x.Name).HasColumnName("Name");
content.Property(x => x.Visits).HasColumnName("Visits");
content.Property(x => x.Date).HasColumnName("Date");
});
});
}
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报