简体   繁体   English

在多个项目中使用相同的数据库

[英]Using the same database in multiple projects

I have 2 projects in my solution: the first one is a ClassLibrary and the second is my Application. 我的解决方案中有2个项目:第一个是ClassLibrary,第二个是我的Application。 I'm using Entity Framework (Code First method) with Sql Server Compact in my second project. 我在第二个项目中使用了Entity Framework(Code First方法)和Sql Server Compact。 The question is how can I use the same database in bith my projects? 问题是如何在我的项目中使用相同的数据库? I've tried to move all my Entities, DbContext and GenericRepository to my Library and I also tried to set connection string inside the code, but I keep receiving IInvalidOperationException: 我试图将我的所有实体,DbContext和GenericRepository移动到我的库中,我也尝试在代码中设置连接字符串,但我一直收到IInvalidOperationException:

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. 无法加载在具有不变名称“System.Data.SqlClient”的ADO.NET提供程序的应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer”。 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. 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882

Here is my app.config: 这是我的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" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0"
           invariant="System.Data.SqlServerCe.4.0"
           description=".NET Framework Data Provider for Microsoft SQL Server Compact"
           type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

尝试检查是否在启动项目中引用了Entity Framework,以及是否在该项目的应用程序配置文件上设置了Entity Framework配置。

The error seems to indicate that there is an assembly reference missing. 该错误似乎表明缺少程序集引用。 Open up the other project, make sure the one with the problem has all the same references. 打开另一个项目,确保有问题的项目具有相同的引用。

Take a look at this similar question Entity Framework Provider type could not be loaded? 看看这个类似的问题实体框架提供程序类型无法加载?

It seems you use Default Connection Factory. 您似乎使用默认连接工厂。 Did you try define connection strings with datasource parameter and indication on .sdf file? 您是否尝试使用.sdf文件中的datasource参数和指示定义连接字符串?

<connectionStrings>
    <add name="ConnectionStringName"
    providerName="System.Data.SqlServerCe.4.0"
    connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" />
</connectionStrings>

If it won't work, I've got one more idea. 如果它不起作用,我还有一个想法。 You're logging in by Windows Authentication. 您正在通过Windows身份验证登录。 Maybe it's not possible to make two parallel connections via the same credentials? 也许不可能通过相同的凭证进行两个并行连接? I'm not sure, but try to check other way than windows authentication. 我不确定,但尝试检查除Windows身份验证之外的其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM