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

标签 c# .net sql-server entity-framework

我的解决方案中有 2 个项目:第一个是 ClassLibrary,第二个是我的应用程序。我在我的第二个项目中使用 Entity Framework(Code First 方法)和 Sql Server Compact。问题是如何在我的项目中使用相同的数据库? 我试图将我所有的实体、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. 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.

这是我的 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 配置。

关于c# - 在多个项目中使用相同的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24086245/

相关文章:

c# - 如何在c#中使用列表中的Except方法

.net - 我应该如何为 Sharepoint 的 .Net 开发创造良好的环境?

php - Haversine 公式 SQL 查询中记录的分组和计数

c# - DNN Web API 服务器错误

c# - 混合 ASP.NET 和 MVC 路由

c# - Linq 在单次迭代中选择和聚合

c# - 集合接口(interface)和 WCF

c# - 如何为unicode字符串的存储过程中的参数添加前缀 'N'

sql-server - 数据库的部分克隆

c# - SQL Server/Entity Framework - 如何按随机数排序然后在以后重现结果?