开发者

Generating multiple rows in the same table from the same object structure

开发者 https://www.devze.com 2023-03-22 12:35 出处:网络
In short, I am trying to take XML data and store it in a table.The structure giving me problems currently looks like so:

In short, I am trying to take XML data and store it in a table. The structure giving me problems currently looks like so:

<Event>
  <PhaseOne>...</PhaseOne>
  <PhaseTwo>...</PhaseTwo>
  <PhaseThree>...</PhaseThree>
  <EventID>12345</EventID>
  <EventDate>09/09/09</EventDate>
</Event>

The end result from this is 3 rows in my Event table (one for each <Phase>). Each row not only requires the data inside the respective <Phase> element, it also requires parent level values such as &l开发者_Python百科t;EventID>.

I have the XSD.exe generated classes to deserialize the XML into but I'm at a loss at how to configure my EF Code First Mappings to generate the correct results.

I thought I would simply map each <Phase> class to the same table but then I'm not sure how I would map the parent level values (<EventID>) to columns in that row.

Unfortunately I cannot change the XML or the table schemas.


You obviously need to separate your deserialized type and your persisted type - they cannot be the same because your persisted type needs different structure then deserialized type have. You will not be able to set the mapping for your deserialized type because EF doesn't support what you want. Your choices are:

  • Create separate types for persistence and fill them from deserialized types
  • Don't use deserialization (or write your own) and instead process XML manually with either XmlDocument, XmlReader or XElement. Fill your persisted types directly from read XML
  • Don't use EF and persist your deserialized types with stored procedures


My solution

I added copies of the EventID and EventDate members to each Phase class and then populated them manually post-deserialization.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号