Entity Structure Code XML First with Data Source

Is it possible to work with Entity Framework (Code First) and have a data source that is an XML file? I need to populate domain objects with values ​​from an XML file.

The XML file has the following structure:

<Person name="John" age="12"> <Products> <Product id="1" name="Product 1" /> <Product id="2" name="Product 2" /> <Product id="3" name="Product 3" /> </Products> </Person> 

C # domain objects have the following structure:

 public class Person { public string Name { get; set; } public int Age { get; set; } public ICollection<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } } 

I can use Linq for XML to parse each XML element and populate objects, but I was looking for a more automatic way to do this (if exists).

+4
source share
2 answers

You will need an XML database accessible through ADO.NET. Otherwise, you intend to implement a custom EF provider (which will continue to be based on ADO.NET) focused on your XML files. EF is designed to access databases.

If you want a more "automated" way, just use XML serialization and its associated attributes - it will be the same as using the code first with data annotations.

+1
source

Entity Framework code is heavily supported by ADO.net .

According to MSDN :

The ADO.NET data provider model provides a common managed interface in the .NET Framework for connecting to and interacting with the data warehouse. The ADO.NET Entity Framework is built on top of the ADO.NET data provider model to allow the use of the Entity Framework with any data source for which a supported provider is available.

+1
source

All Articles