为什么我在我的UI项目中引用EF? [英] Why do I have to reference EF in my UI project?

查看:169
本文介绍了为什么我在我的UI项目中引用EF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是有一个更优雅的方式有实体框架部署你的UI项目(专门为EF SQL Server驱动程序:EntityFramework.SqlServer.dll)部署了您的asp.net项目不必通过添加引用EF的NuGet在你的UI项目?

在这个堆栈溢出问题,发布的答案是让code的不需要线在我的asp.net项目刚拿到的时候我的asp.net应用程序部署在相关的文件复制过来:

<一个href=\"http://stackoverflow.com/questions/20499148/entityframework-sqlserver-not-deployed-in-web-publishework-sqlserver-not-deployed-in-web-publish\"标题=链接>堆栈溢出文章

我不认为我应该补充闲置code,以部署一个依赖。
我也应该有我的UI项目添加到EF引用。

据我了解,EF文件(EntityFramework.SqlServer.dll)在运行时注入到实体框架。
是 - 我的asp.net的项目配置正确 - 它运行伟大的,如果它有一个的NuGet参考EF或DLL EntityFramework.SqlServer.dll

但是:
我不想使用的NuGet到我的asp.net MVC项目只是为了部署该文件的引用添加到实体框架。
我并不想添加一个参考文件,这是在我的DAL项目bin文件夹,那将是非常脆。
我可以一个后生成事件添加到asp.net mvc的项目,但似乎太脆

在MSTest的项目,我们可以添加一个[DeploymentItem ...]属性来部署这些类型的依赖关系。是否有asp.net等效,或许是你可以把在web.config。

我想如果我能在web.config配置为引用由实体框架使用的SQL提供:

 &LT;&的EntityFramework GT;
    &LT; defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework&GT;
      &LT;&参数GT;
        &LT;参数值=11.0/&GT;
      &LT; /参数&GT;
    &LT; / defaultConnectionFactory&GT;
    &LT;供应商&GT;
      &LT;供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/&GT;
    &LT; /供应商&GT;
  &LT; /&的EntityFramework GT;

,你也可以配置DLL部署呢!

2/13编辑

我使用的Visual Studio 2013和EF 6。

一对夫妇,别人问我为什么要在乎我为什么不只是使用的NuGet来管理这一点,使用的NuGet实体框架引用添加到我的UI项目。

2的原因:


  1. 我的UI不应该知道我的ORM的是什么!而且不应该需要在我的UI项目的项目引用它。


  2. 更​​重要的是,我管理一个开发团队与不同技能水平,陆上和离岸。我不想让他们中的一个在UI小心使用EF类。其在UI项目EF的引用使得这一切很容易有在VS或ReSharper的滑使用System.Data.Entity的;和您去使用你的ORM在你的UI项目。



解决方案

在多项目解决方案EF配置问题(S)的说明是在这里:

EntityFramework.SqlServer(或其他机构)没有被复制到本地

它完美阐释我的原因,但我找到了更好的解决方法:

我有一个UI项目和DAL项目的解决方案。

不可以参考EF DLL在UI项目我已经做了:.

1)从UI项目引用删除它。

2)生成后事件添加到UI项目:

 如果$(ConfigurationName)==调试(
复制$(SolutionDir)\\ DBWrapper \\ BIN \\调试\\ $ EntityFramework.SqlServer.dll(SolutionDir)\\ IrregularVerbs \\ BIN \\调试\\

否则,如果$(ConfigurationName)==发布(
复制$(SolutionDir)\\ DBWrapper \\ BIN \\发布\\ $ EntityFramework.SqlServer.dll(SolutionDir)\\ IrregularVerbs \\ BIN \\发布\\

在(字符必须在同一行的如果 - 在其他的方式你会得到错误:

错误2的命令...与code 255退出。

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.

解决方案

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.

这篇关于为什么我在我的UI项目中引用EF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆