Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

asp.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)DBWrapperBinDebugEntityFramework.SqlServer.dll $(SolutionDir)IrregularVerbsBinDebug
)
else if $(ConfigurationName) == Release (
copy $(SolutionDir)DBWrapperBinReleaseEntityFramework.SqlServer.dll $(SolutionDir)IrregularVerbsBinRelease
)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...