Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The type 'SpatialDbContext' does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext.
This is the error showing when I am enabling migrations. What can I try to fix this?
SpatialDbContext is inherited from DbContext,but it still showing above error

What I have tried:

//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))
                    }
                );

        }
    }
}
Posted
Comments
Richard Deeming 23-Aug-22 7:55am    
Search your code for any other classes called SpatialDbContext, in case you have a conflict somewhere.

You could also try changing your constructor to match the recommended pattern:
public SpatialDbContext(DbContextOptions<SpatialDbContext> options) : base(options) { }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900