Entity Framework Core与Entity Framework冲突问题技术咨询
Hey there, I’ve tackled this exact conflict multiple times when migrating .NET Framework apps to ASP.NET Core, so let’s walk through the most practical solutions to get your setup working smoothly.
Core Cause of the Conflict
The root issue here is that your .NET Framework data access layer uses Entity Framework 6 (EF6), while your ASP.NET Core project relies on Entity Framework Core (EF Core). These are two distinct frameworks with overlapping class names (like DbContext) and incompatible APIs, leading to namespace clashes and runtime errors when they’re referenced together.
Solution 1: Isolate EF6 and EF Core with Separate Data Layers
This is the cleanest, most maintainable approach:
- Keep your existing .NET Framework data access layer (using EF6) intact for any legacy code that still needs it.
- Create a new .NET Standard or .NET Core class library specifically for your ASP.NET Core project, and reference only EF Core packages here (
Microsoft.EntityFrameworkCore,Microsoft.EntityFrameworkCore.SqlServer, etc.). - In this new library, define an EF Core-specific
DbContext(inheriting fromMicrosoft.EntityFrameworkCore.DbContext) and map your entities to the same database tables. Example:using Microsoft.EntityFrameworkCore; public class TradeEvoDbContextCore : DbContext { public TradeEvoDbContextCore(DbContextOptions<TradeEvoDbContextCore> options) : base(options) { } // Map your entities here (match EF6's table/column names if needed) public DbSet<ApplicationUser> Users { get; set; } public DbSet<Order> Orders { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // Use Fluent API to match EF6's schema if necessary modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers"); } } - Update your ASP.NET Core
Startup.csto use this EF Core DbContext instead of the EF6 one:services.AddDbContext<TradeEvoDbContextCore>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlDb"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<TradeEvoDbContextCore>();
Solution 2: Resolve Namespace Conflicts (If You Must Share Code)
If you need to reuse entities between the two frameworks without splitting layers:
- Move your entity classes to a .NET Standard class library (compatible with both .NET Framework and .NET Core).
- Avoid using EF6-specific attributes (like
[ComplexType]); instead use EF Core-compatible equivalents (like[Owned]for owned entities) or use conditional compilation if you need framework-specific logic:#if NETFRAMEWORK using System.Data.Entity; [ComplexType] #endif #if NETCOREAPP using Microsoft.EntityFrameworkCore; [Owned] #endif public class Address { public string Street { get; set; } public string City { get; set; } } - In your ASP.NET Core project, always fully qualify EF Core classes to avoid ambiguity with EF6:
// Instead of just DbContext, use the full namespace services.AddDbContext<global::Microsoft.EntityFrameworkCore.DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
Solution 3: Clean Up NuGet References
A common hidden cause is conflicting NuGet packages:
- Ensure your ASP.NET Core project does not reference EF6 packages (like
EntityFramework). Only keep EF Core-related packages. - Your .NET Framework data access layer should only reference EF6 packages (
EntityFramework,EntityFramework.SqlServer). - When referencing the .NET Framework library from ASP.NET Core, enable
PackageReferenceinstead ofpackages.configin the .NET Framework project (this helps avoid package version conflicts).
Key Takeaway
The safest long-term fix is to separate your EF6 and EF Core data layers. This avoids all naming clashes, lets you leverage EF Core’s modern features in ASP.NET Core, and keeps your legacy .NET Framework code working as-is.
内容的提问来源于stack exchange,提问作者Stanislav Balia

