下面是從Getting Started with ASP.NET Core and Entity Framework 6的摘錄:工具兩者的EntityFramework 6和的EntityFramework核心並排的部分,在n層溶液
"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. "
我的目標:是同時實現的EntityFramework 6和的EntityFramework核心側在n層解決方案中並排。
解決方案:
- 核心ASP.NET Web應用程序(.NET框架)與個人用戶帳戶驗證
- 的Windows類庫文件(.dll)包含BLL & DAL
實施EF 6和EF Core並排的原因是:
-
個
- EF核心1.x的執行問題(遷移,許多到零關係)
- 個人用戶帳戶的身份驗證ASP.NET中的核心有Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
- 此使用的依賴 - 案例將實現目前依賴於實體框架BreezeJS 6
我可以成功添加遷移到ASP.NET核心Web應用程序如下:
PM> EntityFrameworkCore\Add-Migration InitialCreate
PM> EntityFramework\Add-Migration InitialCreate
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核心Web應用程序,那麼我就可以成功添加EF6遷移到Windows類:但是,試圖遷移添加到Windows類庫時,我得到一個異常庫的步驟如下:
PM> EntityFramework\enable-migrations
PM> EntityFramework\add-migration InitialCreate
現在我已經在兩個項目中進行了初始遷移。
然而,當我運行ASP.NET核心Web應用程序,我得到了幾個運行時異常:
如果我有我的System.Data.Entity.DbContext的自定義實現參數的構造函數,然後我得到以下運行時異常:
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) …
這是通過刪除默認構造函數來解決的。
當我嘗試註冊一個用戶,下面的異常被拋出:
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
Q - 我在正確的軌道上?現在這只是一個SQL Server身份驗證問題?或者,還有其他問題涉及?
關於如何在EF 6和EF內核上運行.Net 4.6 MVC項目的任何想法?我不想一次性將所有內容遷移到EF內核,但確實需要一些功能來加速導入和批量更新。 – TWilly
[EF核心文檔](https://docs.microsoft.com/en-us/ef/core/)指出EF Core是跨平臺的。我的理解是,爲了實現跨平臺,EF Core必須運行在.NET核心應用程序中。但是,[NuGet](https://www.nuget。org/packages/Microsoft.EntityFrameworkCore /)在依賴項部分列出了.NET Framework 4.5.1。也許有人會說如果可以在.NET 4x應用程序中運行EF Core。 FWIW,您可以找到[功能比較](https://docs.microsoft.com/en-us/ef/efcore-and-ef6/features)以提供信息。 – RandyDaddis