Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Context does not inherit from DbContext in Entity Framework Core

I am trying to keep BooksContext.cs file in a folder called Contexts.

I have a single Book class inside Entities folder. Hence, below is the code in BookContext.cs file.

I have used the following command at Package Manager Console to enable migrations.

PM>Enable-Migrations -ContextTypeName Books.API.Contexts.BooksContext

But, I'm getting below error: The type BooksContext does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext.

Following the error, I am not sure where and how to set DbMigrationsConfiguration.ContextType property

I couldn't get much help from google, and I am not sure what I am missing. Can anyone please help me!

namespace Books.API.Contexts
{
    public class BooksContext : DbContext
    {
        public DbSet<Book> Books { get; set; }

        public BooksContext(DbContextOptions<BooksContext> options)
            : base(options)
        {
            // Tried the accepted answer from below URL, but did not work
            // https://stackoverflow.com/questions/41829229/how-do-i-implement-dbcontext-inheritance-for-multiple-databases-in-ef7-net-co
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Author>().HasData(
                new Author
                {
                    Id = Guid.Parse("e4da4ec7-0fe1-46d8-a133-4374ccd54df9"),
                    FirstName = "George",
                    LastName = "RR Martin"
                },
                new Author
                {
                    Id = Guid.Parse("5afd341b-95df-427a-80df-3ed0995a5da6"),
                    FirstName = "Stephen",
                    LastName = "Fry"
                },
                new Author
                {
                    Id = Guid.Parse("4c8bd0d6-14b1-4284-9be1-1cb78c9fc871"),
                    FirstName = "James",
                    LastName = "Elroy"
                },
                new Author
                {
                    Id = Guid.Parse("fc433048-0153-4230-a15b-df1808de27d6"),
                    FirstName = "Douglass",
                    LastName = "Adams"
                }
            );

            modelBuilder.Entity<Book>().HasData(
                new Book
                {
                    Id = Guid.Parse("92f5d8a9-0141-4bbc-8ee1-61ecdab16cda"),
                    AuthorId = Guid.Parse("e4da4ec7-0fe1-46d8-a133-4374ccd54df9"),
                    Title = "The Winds of Winter",
                    Description = "The book that seems like impossible to write."
                },
                new Book
                {
                    Id = Guid.Parse("1c4ea7c7-f410-4173-b6bd-900f0dd95472"),
                    AuthorId = Guid.Parse("5afd341b-95df-427a-80df-3ed0995a5da6"),
                    Title = "A Game of Throws",
                    Description = "First novel in a song of Ice and Fire"
                },
                new Book
                {
                    Id = Guid.Parse("fd15e575-3d0c-4b92-9b40-63d0f7d58108"),
                    AuthorId = Guid.Parse("4c8bd0d6-14b1-4284-9be1-1cb78c9fc871"),
                    Title = "Mythos",
                    Description = "The Greek myths are amongst the best stories ever told"
                },
                new Book
                {
                    Id = Guid.Parse("d544691c-1a10-4dcd-853a-f7bbd90543ff"),
                    AuthorId = Guid.Parse("fc433048-0153-4230-a15b-df1808de27d6"),
                    Title = "American Tabloid",
                    Description = "It is a 1995 novel"
                }
            );
            base.OnModelCreating(modelBuilder);
        }
    }
}
like image 959
Ashok kumar Avatar asked Jan 25 '23 16:01

Ashok kumar


1 Answers

Small mistake, but good learning for me after spending more than one day painful effort. I hope this will be the good learning for others too.

I have added two NuGet packages of: EntityFramework, and Microsoft.EntityFrameworkCore which is my mistake.

Just adding NuGet package for Microsoft.EntityFrameworkCore will do all the required work.

like image 97
Ashok kumar Avatar answered Feb 01 '23 08:02

Ashok kumar