implement both EntityFramework 6 and EntityFramework Core side-by-side in a n-tier solution

asp.net-core entity-framework-6 entity-framework-core

Question

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.

Solution:

  • ASP.NET Core Web Application (.NET Framework) with Individual User Accounts Authentication
  • Windows Class Libraries (.dll) containing BLL & DAL

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

  • EF Core 1.x implementation issues (migrations, many-to-zero relationships)
  • Individual User Accounts Authentication in ASP.NET Core have a dependency on Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
  • This use-case will implement BreezeJS which currently depends on Entity Framework 6

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=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) ...

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?

1
1
11/12/2016 5:19:56 PM

Accepted Answer

“Exception calling "SetData" with "2" argument(s) exception:”

The “Exception calling "SetData" with "2" argument(s) exception:” can be remedied by passing either -StartupProject or -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.”

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:

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:

PM> EntityFrameworkCore\Update-Database

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

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:

  • I did not need to implement IDbContextFactory.

Now I can successfully perform the following tests:

  • Add a user via the ASP.NET Core MVC Individual User Account scaffolding.
  • Modify the (ASP.NET Core MVC scaffolded) HomeController.About() to persist and retrieve a custom entity to a custom System.Data.Entity.DbContext implementation.
  • Modify About.cshtml to display the persisted custom entity.
1
11/12/2016 6:08:20 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow