【发布时间】:2018-11-09 15:20:51
【问题描述】:
我有一个用于 MVC 应用程序的存储库。它运行良好,没有任何问题。
目前,我正在使用 .Net Core 构建一个新的客户端应用程序。我引用了具有目标框架 4.6.1 的现有存储库项目。我可以毫无问题地构建。
当我尝试使用现有的数据库上下文访问数据时。我收到一个错误,它找不到 DefaultConnection 字符串。
public class MyAppContext : DbContext
{
public MyAppContext():base("name=DefaultConnection"){}
这是我在 config.json 中的连接
"ConnectionStrings": {
"DefaultConnection": "Data Source=[SERVER]; Initial Catalog=[CATALOG]; User ID=[USERID];
Password=[PASSWORD];MultipleActiveResultSets=true;" },
我向我的 .Net Core 应用程序添加了一个 app.config 文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlClient" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=[SERVER]; Initial Catalog=[CATALOG]; User ID=[USERID];
Password=[PASSWORD];MultipleActiveResultSets=true; " providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
现在我在下面遇到错误
消息“实体框架提供程序类型 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' 在应用程序配置文件中注册 对于具有不变名称“System.Data.SqlClient”的 ADO.NET 提供程序 无法加载。确保程序集限定名称是 并且该程序集可用于正在运行的应用程序。 更多信息请见http://go.microsoft.com/fwlink/?LinkId=260882 信息。”字符串
我尝试将 .net EntityFramework 安装到我的 .net 核心应用程序中,但收到一条警告说它不完全兼容,而且我收到了同样的错误,只是版本更短。
如何使 .Net Core Web 应用程序共享 .Net 4.6 库而不重写它们以使用 .Net Core?
【问题讨论】:
-
我认为您的选择是使用数据层作为 .Net 标准或使用 EF Core.. 检查What is the difference between .NET Core and .NET Standard Class Library project types?
标签: .net-core entity-framework-6 entity-framework-core asp.net-core-2.0 .net-4.6