0

I have an n-layered application with data base activity performed in my data access layer. I have an application layer which asks my data access layer to perform tasks from the repositories in the data access layer. My user interface, which for now is a simple console app to test results, asks my application layer to get things like a list of data which in turn the application layer gets from the repository and it all gets back to the console app.

If I don't add the entity framework as a reference in my console app, I get the following error:

The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

Why am I getting this error when the console app makes no data access calls or entity framework operations? All that is done in my data access layer which does have Entity Framework referenced.

Update: Below is my console interface:

class MyServices
{
    IProductRequestServices _ProductRequestServices;
    public MyServices(IProductRequestServices _ProductRequestServices)
    {
        this._ProductRequestServices = _ProductRequestServices;
    }
    public void ProductList()
    {
        List<ProductRequestDetailDto> aList = _ProductRequestServices.GetProductRequestExtendedDetailAll();
        foreach (ProductRequestDetailDto prodReq in aList)
        {
            System.Console.WriteLine("Product Req ID: {0} - Product Name: {1}",
                prodReq.productRequestId.ToString(), prodReq.productName);
        }
    }
    public void ClientList()
    {
        List<ProductRequestDetailDto> aList = _ProductRequestServices.GetProductRequestExtendedDetailAll();
        foreach (ProductRequestDetailDto prodReq in aList)
        {
            System.Console.WriteLine("Product Req ID: {0} - Product Name: {1}",
                prodReq.productRequestId.ToString(), prodReq.firstName + " " + prodReq.lastName);
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
     ProductRequestServices _ProductRequestServices = new ProductRequestServices();

     MyServices MyServices = new MyServices(_ProductRequestServices);

        MyServices.ProductList();
        System.Console.WriteLine("============================");
        MyServices.ClientList();
        System.Console.ReadLine();
    }
}

Below is the App.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings>
    <add name="MDISContext" connectionString="metadata=res://*/ModelEntities.csdl|res://*/ModelEntities.ssdl|res://*/ModelEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WIN-2012-SRVR-3;initial catalog=MDIS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

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

If I just remove the entityFramework sections and database strings I get the following error:

{"Schema specified is not valid. Errors: \r\nModelEntities.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information."}

Then if I remove the configuration section which does an entityframework registstration I get the same error.

All these errors go away if I add EntityFramework to the consoled project of my solution which also adds these entries into the app.config

4

2 回答 2

4

存储库层将在为正在运行的应用程序域加载的配置文件中查找 EF 配置。所以是的,您的控制台应用程序需要配置并且需要 EF 引用。如果您的存储库位于外部服务或其他进程中,那么您将不需要 EF 引用。

于 2016-04-11T00:38:02.950 回答
0

只有数据/存储库层应该知道与 Db 相关的 dll/逻辑,但我在实体框架方面遇到了同样的问题。MSBuild 没有在 bin/debug 文件夹中复制此 EF SQL dll。我必须在我的 UI 项目中包含这个 dll。

原因,如果我没记错的话,是因为 MSBuild 试图做一些智能的事情,那就是:如果它无法在某种依赖树中找到引用,它不会将它包含在输出或 bin/debug 文件夹中. (我不记得这个的来源)

于 2016-04-11T00:49:00.790 回答