Here is an excerpt from Getting Started with ASP.NET Core and Entity Framework 6:
"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. "
My Goal: is to implement both EntityFramework 6 and EntityFramework Core side-by-side in a n-tier solution.
The reason for implementing EF 6 and EF Core side-by-side is due to:
I can successfully add migrations to the ASP.NET Core Web Application as follows:
PM> EntityFrameworkCore\Add-Migration InitialCreate
However, I get an exception when attempting to add migrations to the Windows class library:
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=22.214.171.124, 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) ...
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.
However, when I run the ASP.NET Core Web Application I get a couple of runtime exceptions:
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 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): 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 - Am I on the right track? Is this now simply a SQL Server authentication issue? Or, are other issues involved?
â€œException calling "SetData" with "2" argument(s) exception:â€ can be remedied by passing either
-StartupProjectName parameter in the following commands:
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): 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:
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
In the Package Manager Console, set the Default Project to the class library containing the EF Core context and execute:
At this point, I can inspect the databases using either Server Explorer or SQL Server Object Explorer.
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:
Now I can successfully perform the following tests:
The EF6 commands don't know how to work with ASP.NET Core projects (they didn't exist when it was written). You'll probably have to unload these projects before running EF6 commands.
You may also be able to explicitly specify
-StartupProject as the EF6 project to workaround the issue.