MxFragz
MxFragz

Reputation: 198

Entity Framework & Portable Class Library

I have a solution containing a WPF Project and a Windows Phone Project, both having a reference to my PCL that contains my Models and ViewModels. I also have a WCF Project that allows me to call a common API. It worked just fine so far.

Now I want to use Entity Framework to communicate with an SQL database so I can make request from my ViewModels. I thought about moving my Models from the PCL to the WCF project, and use them as entities. This implies to reference my WCF project in the PCL.

Just like specified here: Entity Framework for Portable Class Library , I get the following error:

"Failed to add reference to 'System.ComponentModel.DataAnnotations'. Please make sure that it is in the Global Assembly Cache."

which is in reality a Nuget issue. (http://nuget.codeplex.com/workitem/2978). The real reason is that Entity Framework is not compatible with PCLs anymore. From what I read, the Entity Framework 7 will allow me to do so, but it's impossible for now.

Does anyone know a good way to communicate with my database with this configuration ?

Upvotes: 2

Views: 2946

Answers (2)

MxFragz
MxFragz

Reputation: 198

Here is my solution:

My Models and ViewModels are still defined in my PCL. Entities are defined in the WCF. I added a class in my WCF that converts an Entity to a Model. An interface that defines each available request to the API in the PCL, and I'm implementing this interface in each platform. Each platform implementation just calls the methods defined in WCF (which means I have to implement every request identically in each platform). I'm using my conversion class in WCF to return a model instead of an entity.

It's not the best solution, but it works while I'm waiting for Entity Framework 7.

Upvotes: 1

Scott Nimrod
Scott Nimrod

Reputation: 11595

I usually store my model within a wcf application behind the services layer.

As a result, my model has no dependencies on UI dlls.

Upvotes: 0

Related Questions