我有一个模型:
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();
}
数据库表有列:
Index
Name
Visits
Date
我想知道,有没有办法用一些EF属性来标记我的模型的属性,以将对象直接保存到数据库并避免使用像这样的额外模型?
public class PersonModelDatabase
{
public int Index { get; set; }
public string Name { get; set; }
public int? Visits { get; set; }
public DateTime Date { get; set; }
}
FFIVE
相关分类