启用EF迁移时SpatialDbContext继承验证报错的解决求助
问题描述
执行迁移启用命令:
Enable-Migrations -ContextTypeName SpatialSample.SpatialDbContext
出现错误提示:
The type 'SpatialDbContext' does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext.
但SpatialDbContext类确实继承自DbContext,相关代码如下:
//using Microsoft.EntityFrameworkCore; using SpatialSample.Entities; //using DbContext = Microsoft.EntityFrameworkCore.DbContext; //using Microsoft.EntityFrameworkCore.Migrations; //using GeoAPI.Geometries; using NetTopologySuite; using NetTopologySuite.Geometries; using System; //using System.Data.Entity; using Microsoft.EntityFrameworkCore; //using Microsoft.EntityFrameworkCore; //using Microsoft.EntityFrameworkCore; //using Coordinate = NetTopologySuite.Geometries.Coordinate; //using Coordinate = GeoAPI.Geometries.Coordinate; //using System.Data.Entity; //using System.Data.Entity.Infrastructure; //using Microsoft.AspNet.Identity.EntityFramework; namespace SpatialSample { public class SpatialDbContext : DbContext //DbContext { // public Microsoft.EntityFrameworkCore.DbSet<TouristAttraction> TouristAttractions { get; set; } // public DbSet<TouristAttraction> TouristAttractions { get; set; } public DbSet<TouristAttraction> TouristAttractions { get; set; } // public SpatialDbContext() : base() { } public SpatialDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); //GeometryFactory gf = new GeometryFactory(); modelBuilder.Entity<TouristAttraction>() .HasData( new TouristAttraction { Id = 1, Name1 = "Taj Mahal", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(27.175015, 78.042155)) }, new TouristAttraction { Id = 2, Name1 = "The Golden Temple of Amritsar", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(31.619980, 74.876485)) }, new TouristAttraction { Id = 3, Name1 = "The Red Fort, New Delhi", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(28.656159, 77.241020)) }, new TouristAttraction { Id = 4, Name1 = "The Gateway of India, Mumbai", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(18.921984, 72.834654)) }, new TouristAttraction { Id = 5, Name1 = "Mysore Palace", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(12.305025, 76.655753)) }, new TouristAttraction { Id = 6, Name1 = "Qutb Minar", Location = (GeoAPI.Geometries.IPoint)geometryFactory.CreatePoint(new Coordinate(28.524475, 77.185521)) } ); } } }
修复方法
切换到EF Core迁移命令:
Enable-Migrations是EF6的命令,而你的代码使用的是EF Core的DbContext。EF Core无需提前启用迁移,直接使用以下命令:Add-Migration InitialCreate -Context SpatialSample.SpatialDbContext Update-Database -Context SpatialSample.SpatialDbContext清理包与命名空间冲突:检查项目NuGet包,移除EF6相关的
EntityFramework包,只保留EF Core系列包(如Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.Tools等)。同时删除代码中被注释的EF6命名空间引用,避免潜在冲突。确认启动项目与框架兼容:执行迁移命令时,确保选中的启动项目是包含
SpatialDbContext的项目,且项目目标框架版本与所用EF Core包的兼容。
内容的提问来源于stack exchange,提问作者Bobin Scaria
相关产品推荐
相关产品推荐

