亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 EF 保存實體并避免創建新模型?

如何使用 EF 保存實體并避免創建新模型?

C#
慕萊塢森 2023-07-22 16:30:23
我有一個模型:public class PersonModel{    [Key]    [JsonProperty("ix")]    [XmlElement("ix")]    public int Index { get; set; }    [XmlElement("content")]    public ContentModel Content { get; set; }}[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; }    [XmlElement("date")]    public string dateRequested    {        get { return Date.ToString("yyyy-MM-dd"); }        set { Date = DateTime.ParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); }    }}他就是這樣,因為我想要xml一棵看起來像這樣的樹:<?xml version="1.0" encoding="utf-8"?><PersonXmlModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <ix>5</ix>  <content>    <name>Jadon</name>    <date>2009-12-21</date>  </content></PersonXmlModel>但我也想在使用時將對象保存到數據庫EF:public void AddItem(PersonModel request){    PersonModel dataItem = new PersonModel    {        Index = request.Index,        Content = new ContentModel        {            Name = request.Content.Name,            Visits = request.Content.Visits,            Date = request.Content.Date        }    };    _context.Requests.Add(dataItem);    _context.SaveChanges();}數據庫表有列:IndexNameVisitsDate我想知道,有沒有辦法用一些EF屬性來標記我的模型的屬性,以將對象直接保存到數據庫并避免使用像這樣的額外模型?public class PersonModelDatabase{    public int Index { get; set; }    public string Name { get; set; }    public int? Visits { get; set; }    public DateTime Date { get; set; }}
查看完整描述

1 回答

?
FFIVE

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");

? ? ? ? ? ? ? ? });

? ? ? ? });

? ? }


查看完整回答
反對 回復 2023-07-22
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號