Jak przypisać własności modelu poszczególnym własnościom bazy w EF?

0

Hej, mam model:

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

A wygląda on tak bo chcę mieć drzewo xml które wygląda tak:

<?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>

Aczkolwiek chcę zapisać też obiekt do bazy przy użyciu 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();
        }

Ale tabela w bazie posiada jedynie kolumny:

Index
Name
Visits
Date

I zastanawiam się czy w modelu da się (jakoś) dzięki EF przypisać nazwy kolumn z pominięciem drzewa modelu bez tworzenia nowego modelu takiego jak ten?

public class PersonModelDatabase
    {
        public int Index { get; set; }
        public string Name { get; set; }
        public int? Visits { get; set; }
        public DateTime Date { get; set; }
    }
0

W odpowiedzi na swoje pytanie skorzystałem z ComplexType, a konkretnie podejścia z tej odpowiedzi na SO rozwiązują problem.

Próbując ich wcześniej załapałem się później na tym, że public string dateRequested rzucał mi wyjątkiem:

Invalid column name 'ContentModel_dateRequested'.

Dlatego musiałem jeszcze dodać do niego [NotMapped]

1 użytkowników online, w tym zalogowanych: 0, gości: 1