0

Using Microsoft Entity Framework and MVC, I have the following requirement, implement an entity where some properties belong to a table within a sql server DB, and other properties are loaded from an XML column on the same table, I'd like to create properties in order to expose some values stored in the XML to the MVC view and controller.

Let's say I have the Entity "Laptop", loading columns from a table like processor and ram; for flexibility this table has an XML column with more data, I'd like to expose using properties of the "Laptop" Entity some of the values within the XML, like dvd, bluetooth, usb, etc.

I tried using a partial class with the name "Laptop", trying to declare and add the routines for the maintenance of the additional properties, however this is not working, the additional properties declared within this partial class are not visible on the "Laptop" entity for upper layers like MVC.

My problem is not related on how to extract the values from an XML, rely more on how to combine an Entity with custom properties, the entity is created automatically updating the model, I need a way to define the other properties somewhere else, not deleted automatically by the IDE at design time, for this reason my first thought was a partial class.

Any ideas?

1 Answers1

0

You will need some sort of a dynamic object, so try the ExpandoObject class. You might have to use the ExpandoObject in conjunction with LINQ Expression trees for taking the data in the XML column and transforming it into a fieldname,fieldvalue array or another dynamic object.

Here's an example that might fit your use case:

Shiva
  • 20,575
  • 14
  • 82
  • 112