MVC 2 and Entity Framework. Should I put Entity in a separate layer?

I am trying to find some information about the preferred solution setup when using MVC 2 and Entity Framework, and it would seem to me the most intuitive to install this web application in 3 layers:

  • MyProject.Web (MVC project for presentation)
  • MyProject.Data (Data Gateway Layer Using the Entity Framework for Working with the Database)
  • MyProject.Tests (test project created when setting up a new MVC project)

This seems to contradict the examples I find and the documentation (for example, the NerdDinner example) that view the MVC project as a mediation directly with the database. Example NerdDinner puts data access in a repository class mixed with MVC models.

I tried to take the path that seems best to me and created my "ADO.NET Entity Data Model" element in my separate Data project, but this gives me an error when I try to use MVC to list the elements in it:

"Unable to load the specified metadata resource."

if I don't have a copy of the entity data model in my MVC project.

Before I go too far along the path of studying this error, I want to find out if I am against fighting the framework of purism, when they simply discipline me, only using access to data in my repository.

like this: - Is it possible or recommended to put my Entity Framework in this other project? “Will I sacrifice some other MVC features, dividing it this way?” (for example, checking?) - If I am heading in the right direction and others agree, are there any other examples or documents that someone could point me to?

+4
source share
2 answers

Yes, I consider it a good idea to place your objects in a separate assembly.

One way to fix the "Unable to load the specified metadata resource" error is to explicitly specify the assembly in the connection string:

 <connectionStrings> <add name="MyEntities" connectionString="metadata=res://*/AssemblyName.bin.Namespace.MyEntities.csdl|res://*/AssemblyName.bin.Namespace.MyEntities.ssdl|res://*/AssemblyName.bin.Namespace.MyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SERVER_NAME;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/> </connectionStrings> 

Pay particular attention to AssemblyName.bin.Namespace.MyEntities . This is the name of the resource assigned to the assembly (assuming the assembly is called AssemblyName.dll). You may need to use Reflector to figure this out the first time you do it.

This answer may also be helpful.

+1
source

Of course, you can put the Entity Framework definition in another project. Personally, I keep it in another project if the data layer will need to be used by several interfaces (MVC, WCF, WPF).

Take a look at these two MSDN articles on creating and using EntityConnection.

Create EntityConnection

Use EntityConnection with Object Context

0
source

Source: https://habr.com/ru/post/1303582/


All Articles