SeToY
SeToY

Reputation: 5895

Sharing EntityFramework Datamodel

I'm using EntityFramework 4 in my WPF desktop-application (NS: MyCompany.MyProduct).

Now I want to create the same application in ASP.NET (NS: MyCompany.MyProduct2), with the exact same functionality... Hence I need to use the exact same database as the WPF application already does.

Additionally, I want to create a new executable (hence a new wpf project) on top of my primary WPF project, that also uses the same ConnectionString like the WPF / ASP.NET-Application, to display some reports.

So I figured out I'd need to share the .edmx-Model (NS: MyCompany.MyProduct.Models.DBModel.edmx) and the ConnectionString that is already persistent in the app.config of the WPF app or the web.config of the ASP.NET-App.

What is the best or recommended way to do this?

Upvotes: 0

Views: 113

Answers (3)

Olly
Olly

Reputation: 6026

Agree with @James here. Don't be afraid of adding library projects to your solution. So you would have a project called MyCompany.Model that contains your EDMX. (Actually, you might find later that you want to use the T4 generation to split your model off from your DbContext or ObjectContext, but that's another discussion.)

With Visual Studio you can actually add a project--your EDMX project--to more than one solution. Be careful not to make changes to the EDMX project when editing one solution that break the other, though.

Respectfully, you may find that it's not ideal to use the GAC here, especially if your EDMX is still evolving.

As for connection strings, these are one thing that you tend not to share between projects. Typically they are in the app.config (or web.config) for your executable project. This can be a gotcha, because if you use a library project to hold your EDMX, EF will automatically create an app.config in the library project, with the connection string in it. But .NET never uses an app.config for a DLL. The only reason it's there is to give you something you can copy/paste into the real app.config for your executable (WPF) app.config or the web.config.

Upvotes: 1

James
James

Reputation: 82096

What is the best or recommended way to do this?

Create a class library project and put EF model in there and share it between your WPF/Web projects. The app.config file of a library project isn't picked up by the parent project therefore you will have to manually update your web.config file to add the ConnectionString section.

This approach allows you to share business logic between your WPF app & your web app. If they are essentially the same app but on different platforms, then you should only be re-implementing the UI - this is one of the major advantages of the MVC pattern.

Upvotes: 1

Steve Stokes
Steve Stokes

Reputation: 1230

If your goal is to share the single .edmx dll between all three applications on one machine, the best way to accomplish this is to sign the dll, then add it to the GAC. If the dll will remain on different servers, there is no need to GAC the dll, you can just reference it in your projects, and add the connectionstring entry in the respective .configs.

GAC: http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.100).aspx

Install a DLL to the GAC: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx

Upvotes: 0

Related Questions