David Archer David Archer - 3 months ago 58
C# Question

Using XML to store data, how can I use Entity Framework?

I'm building a program which has two options: an offline and an online storage option. The online stuff isn't a problem, never has been and probably never will be. To make things a little more simple, I am using the Entity Framework with my online database to do all of my CRUD.

The offline option is where I'm having a problem. I tried adding a local SQL install as part of the install package, but it was failing on certain computers depending upon which version of SQL I was using, some people just couldn't install it at all... honestly, I don't want to be going through that just to get people to install the program. I want to do two things:

1) Store the data. I'm currently thinking Linq to XML is my best bet with nothing extra to install, but is there any way to map the entities to classes within the program that would take care of the XML CRUD?

2) Sync the data. I'm thinking I'll use the Sync Framework, but again, with no normal database in the background, it's a bit of a pain.

Any help/advice/comments very welcome, if there's a much easier way of doing all this, please let me know.

Answer

I'm glad to hear you've got something figured out for the syncing logic. That's the bit that would keep me up at night. GUIDs are your friend in a situation like this.

And I've never attempted to use 1 ObjectContext targeted at different datasources. Maybe you can pull off something slick using OLEDB as your provider. I'd go a little down that road. But I suspect you'll find it's more trouble than it's worth.

I recommend following the Repository Pattern. Then you can just implement a XMLRepository and a SQLRepository. This may mean some borderline double coding. But queries against XML versus a database have their own concerns. So, depending on your specifics, I think violating the DRY principle may be justified.

Also, and I acknowledge this is completely subjective, it strikes me that the Entity Framework is overkill for a relational model that can be easily persisted in XML. I'm just curious if you considered using LINQ-to-SQL. And if so, what convinced you to go the way of EF?

Comments