在n层解决方案中并排实现EntityFramework 6和EntityFramework Core [英] implement both EntityFramework 6 and EntityFramework Core side-by-side in a n-tier solution

查看:141
本文介绍了在n层解决方案中并排实现EntityFramework 6和EntityFramework Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下摘录自 ASP.NET入门核心和实体框架6 :

在ASP.NET Core 1.0中使用Entity Framework 6的推荐方法 应用程序是将EF6上下文和模型类放在一个类中 面向整个库的库项目(.csproj项目文件) 框架.从ASP.NET Core添加对类库的引用 项目. "

"The recommended way to use Entity Framework 6 in an ASP.NET Core 1.0 application is to put the EF6 context and model classes in a class library project (.csproj project file) that targets the full framework. Add a reference to the class library from the ASP.NET Core project. "

我的目标:是在n层解决方案中并排实现EntityFramework 6和EntityFramework Core.

My Goal: is to implement both EntityFramework 6 and EntityFramework Core side-by-side in a n-tier solution.

解决方案:

  • 具有单个用户帐户身份验证的ASP.NET Core Web应用程序(.NET Framework)
  • 包含BLL&的Windows类库(.dll) DAL

并排实施EF 6和EF Core的原因是:

The reason for implementing EF 6 and EF Core side-by-side is due to:

  • EF Core 1.x实施问题(迁移,多对零关系)
  • ASP.NET Core中的个人用户帐户身份验证依赖于Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
  • 该用例将实现BreezeJS,当前依赖于Entity Framework 6

我可以成功地将迁移添加到ASP.NET Core Web应用程序,如下所示:

I can successfully add migrations to the ASP.NET Core Web Application as follows:

PM> EntityFrameworkCore\Add-Migration InitialCreate

但是,尝试将迁移添加到Windows类库时出现异常:

However, I get an exception when attempting to add migrations to the Windows class library:

PM> EntityFramework\Add-Migration InitialCreate

使用"2"个参数调用"SetData"的异常:类型 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' 在程序集"Microsoft.VisualStudio.ProjectSystem.VS.Implementation"中, 版本= 1.4.1.0.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a'是 未标记为可序列化." C:\ Src \ Hcs.NetCore \ packages \ EntityFramework.6.1.3 \ tools \ EntityFramework.psm1:720 字符数:5 + $ domain.SetData('startUpProject',$ startUpProject) ...

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Src\Hcs.NetCore\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720 char:5 + $domain.SetData('startUpProject', $startUpProject) ...

如果卸载ASP.NET Core Web应用程序,则可以通过以下步骤将EF6迁移成功添加到Windows类库中:

If I unload the ASP.NET Core Web Application, then I can successfully add EF6 migrations to the Windows class library with the following steps:

PM> EntityFramework\enable-migrations
PM> EntityFramework\add-migration InitialCreate

现在我已经在两个项目中都进行了初始迁移.

Now I have the initial migration in both projects.

但是,当我运行ASP.NET Core Web应用程序时,我遇到了几个运行时异常:

However, when I run the ASP.NET Core Web Application I get a couple of runtime exceptions:

如果我在System.Data.Entity.DbContext的自定义实现中具有无参数的构造函数,则将收到以下运行时异常:

If I have parameter-less constructor in my custom implementation of System.Data.Entity.DbContext, then I get the following runtime exception:

System.Data.SqlClient.SqlException未通过用户代码处理...
Message =与网络相关或特定于实例的错误发生在 建立与SQL Server的连接.找不到服务器或 无法访问.验证实例名称正确,并且 SQL Server配置为允许远程连接. (提供者:SQL 网络接口,错误:26-错误定位服务器/实例 指定)…

System.Data.SqlClient.SqlException was unhandled by user code …
Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) …

已通过删除默认构造函数对此进行了补救.

That was remedied by removing the default constructor.

当我尝试注册用户时,抛出了以下异常:

When I attempted to register a user, the following exception was thrown:

System.Data.SqlClient.SqlException(0x80131904):无法打开数据库 登录名请求的"xxx".登录失败.用户登录失败 yyy \ zzz'.在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 身份,SqlConnectionString connectionOptions,SqlCredential 凭证,对象providerInfo,字符串newPassword,SecureString newSecurePassword,布尔型redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData, DbConnectionPool池,字符串accessToken,布尔值

System.Data.SqlClient.SqlException (0x80131904): Cannot open database "xxx" requested by the login. The login failed. Login failed for user yyy\zzz'. at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) at

问-我走对了吗?现在这仅仅是一个SQL Server身份验证问题吗?或者,是否涉及其他问题?

Q - Am I on the right track? Is this now simply a SQL Server authentication issue? Or, are other issues involved?

推荐答案

使用"2"个参数异常调用"SetData"的异常:"

可以通过在以下命令中传递-StartupProject-StartupProjectName参数来补救Exception calling "SetData" with "2" argument(s) exception::

在程序包管理器控制台中,将默认项目"设置为包含EF6上下文的类库(在我的情况下为Dna.Net.Application.DAL.EF6)并执行:

In the Package Manager Console, set the Default Project to the class library containing the EF6 context (Dna.Net.Application.DAL.EF6 in my case) and execute:

PM> EntityFramework\enable-migrations -StartupProject Dna.Net.App.DAL.EF6
PM> EntityFramework\add-migration InitialCreate -StartupProjectName Dna.Net.App.DAL.EF6

"System.Data.SqlClient.SqlException(0x80131904):无法打开登录请求的数据库"xxx".登录失败."

在运行时,可以通过在执行时通过单击 [Apply Migrations] 按钮保存时单击 [Apply Migrations] 按钮来纠正System.Data.SqlClient.SqlException (0x80131904): Cannot open database "xxx requested by the login. The login failed.异常. strong> Update-Database 如下:

"System.Data.SqlClient.SqlException (0x80131904): Cannot open database "xxx" requested by the login. The login failed."

The "System.Data.SqlClient.SqlException (0x80131904): Cannot open database "xxx" requested by the login. The login failed." exception can be remedied at runtime by clicking the [Apply Migrations] button when saving a new user account, or, at design-time by executing Update-Database as follows:

在程序包管理器控制台中,将默认项目"设置为包含EF6上下文的类库,然后执行:

In the Package Manager Console, set the Default Project to the class library containing the EF6 context and execute:

PM> EntityFramework\Update-Database -StartupProject Dna.Net.Application.DAL.EF6

在程序包管理器控制台中,将默认项目"设置为包含EF Core上下文的类库,然后执行:

In the Package Manager Console, set the Default Project to the class library containing the EF Core context and execute:

PM> EntityFrameworkCore\Update-Database

这时,我可以使用服务器资源管理器或SQL Server对象资源管理器检查数据库.

At this point, I can inspect the databases using either Server Explorer or SQL Server Object Explorer.

Update-Database命令为EFCore和EF6上下文创建了唯一的数据库.

The Update-Database commands have created unique databases for the EFCore and EF6 contexts.

此解决方案遵循

This solution follows the guidelines contained in the Sample Application section of Getting Started with ASP.NET Core and Entity Framework 6 with the following exception:

  • 我不需要实现IDbContextFactory.

现在我可以成功执行以下测试:

Now I can successfully perform the following tests:

  • 通过ASP.NET Core MVC个人用户帐户支架添加用户.
  • 修改(安装了ASP.NET Core MVC的)HomeController.About()以持久保存并检索自定义实体到自定义System.Data.Entity.DbContext实现.
  • 修改About.cshtml以显示持久的自定义实体.

这篇关于在n层解决方案中并排实现EntityFramework 6和EntityFramework Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆