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

后端 未结 5 1832
生来不讨喜
生来不讨喜 2020-12-17 18:16

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 w

相关标签:
5条回答
  • 2020-12-17 19:02

    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.

    0 讨论(0)
  • 2020-12-17 19:06

    Your reasoning is somewhat moot, but I can help clarify.

    If you want to limit exposure to your EF context, then expose an interface via a unit-of-work/repository pattern. Create an abstraction layer on top of your EF implementation that gets injected via some DI construct like Ninject or Autofac.

    Either way, the only way developers won't use your EF context is to be disciplined developers. Code reviews will help alleviate people doing dumb stuff.

    0 讨论(0)
  • 2020-12-17 19:14

    What version of EF are you using? I have the same exact setup as you are describing... MVC proj + EF DAL proj based on EF5. I checked the references on both projects, and neither of them refer to EntityFramework.SqlServer.dll. Both only reference EntityFramwork.dll.

    Regardless, I get the basic pain point you are describing about referencing/publish dependencies. However, unlike you, I have been content to let NuGet do the work, and I don't mind that.

    I just Right-Click on the Solution, and click "Manage NuGet Packages for Solution..." and then I choose the Installed packages node in the left pane, scroll to Entity Framework and click on it, then click Manage button and check it to apply to the MVC project and the DAL project.

    0 讨论(0)
  • 2020-12-17 19:15

    Add this code to let visual studio understand you are using entityframework.sqlserver in code. EveryThing will become good after entering this code to your project with EF refrence.

    public abstract class dummy: DbContext
    {
         static dummy()  
         {
              var dummyvariable= 
                    System.Data.Entity.SqlServer.SqlProviderServices.Instance;
         }
    }
    
    0 讨论(0)
  • 2020-12-17 19:16

    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.

    0 讨论(0)
提交回复
热议问题