Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to reference EF in my UI project?

Isn't there a more graceful way to have Entity Framework deployed with your UI project (specifically the sql server driver for EF: EntityFramework.SqlServer.dll) deployed with your asp.net project than having to add reference to EF via NuGet in your UI project?

In this stack overflow question, the answer posted is to make an unneeded line of code in my asp.net project just to get the dependent file to copy over when my asp.net app is deployed:

stack overflow article

I don't think I should have to add unused code in order to deploy a dependency. Nor should I have to add a reference to EF in my UI project.

I understand that the EF file (EntityFramework.SqlServer.dll) is injected at run time into entity framework. Yes - my asp.net project is configured correctly - it runs great if it has a nuget reference to EF or the dll EntityFramework.SqlServer.dll.

But: I don't want to add a reference to entity framework using nuget to my asp.net mvc project just to deploy this file. I don't want to add a reference to the file, which is in my DAL project bin folder, that would be very brittle. I could add a post build event to the asp.net mvc project, but that seems brittle too.

In MSTest projects we can add a [DeploymentItem ...] attribute to deploy these types of dependencies. Is there an equivalent in asp.net, perhaps something you can put in the web.config.

I would think that if I could configure in the web.config to reference the sql provider used by entity framework:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

That you could also configure the dll to deploy too!

2/13 Edit

I am using visual studio 2013 and EF 6.

A couple of people have asked why I care and why I don't just use NuGet to manage this and add the entity framework reference to my UI project using NuGet.

2 Reasons:

  1. My UI should not have to know anything about what my ORM is! And should not need a project reference to it in my UI project.

  2. More importantly, I manage a team of developers with varying skill levels, onshore and off shore. I don't want one of them to accidentally use an EF class in the UI. Having a reference to EF in the UI project makes it all to easy to have VS or Resharper slip in a "using System.Data.Entity;" and off you go using your ORM in your UI project.

like image 970
Cameron Avatar asked Feb 13 '14 05:02

Cameron


2 Answers

I have encountered the same problem and found the solution.

  1. Remove EF references in the UI layer.
  2. Remove EF references in the web.config
  3. Just make sure to define the connection string

  <connectionStrings>
    <add name="PlayData" connectionString="Data Source=.\SQLExpress;Initial Catalog=PlayHarder;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>  

There! No more reference to EF in the UI layer, and the web page will still run. Use only EF in the data layer via repositories and unit of work pattern.

To summarize: Just do NOT install or nuget-install EntityFramework in the UI layer and it will work just fine.

like image 79
Windel Marquez Avatar answered Nov 16 '22 08:11

Windel Marquez


Explanation of issue(s) with EF configuration in multi project solution is here:
EntityFramework.SqlServer (or other providers) not being copied locally
It perfect explain me the reasons, but I found better workaround:
I have a solution with UI project and DAL project.
To not reference EF dll's in UI project I have done:.
1) removed it from UI project references.
2) add post-build event to UI project:

if $(ConfigurationName) == Debug (
copy $(SolutionDir)\DBWrapper\Bin\Debug\EntityFramework.SqlServer.dll $(SolutionDir)\IrregularVerbs\Bin\Debug\
)
else if $(ConfigurationName) == Release (
copy $(SolutionDir)\DBWrapper\Bin\Release\EntityFramework.SqlServer.dll $(SolutionDir)\IrregularVerbs\Bin\Release\
)

the "(" char must be in the same line as "if" - in other way you get error:
Error 2 The command "..." exited with code 255.

like image 23
user3057544 Avatar answered Nov 16 '22 09:11

user3057544