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 關注
- 114 瀏覽
添加回答
舉報