locked
My Context does not inherit from DbContext in Entity Framework Core RRS feed

  • Question

  • User1768510565 posted

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

    Wednesday, October 30, 2019 6:17 AM

Answers

  • User1768510565 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 1, 2019 4:30 AM

All replies

  • User-821857111 posted

    You don't seem to have declared a DbSet property to represent your Author entity.

    Wednesday, October 30, 2019 7:11 AM
  • User1768510565 posted

    I don't want to represent separate Author entity, Of course, I can get Authors for Books from Books entity.

    Wednesday, October 30, 2019 8:41 AM
  • User753101303 posted

    Hi,

    Wondering if it could be some kind of mismatch between EF Core and EF 6.3? The DbContext you are using is in which namespace?

    Wednesday, October 30, 2019 8:51 AM
  • User-17257777 posted

    Hi kak.mca,

    Migrations are enabled by default in EF Core. You can directly use Add-Migration to add a new migration.

    For more details, you can refer to

    https://www.learnentityframeworkcore.com/migrations

    Best Regards,

    Jiadong Meng

    Thursday, October 31, 2019 8:07 AM
  • User1768510565 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 1, 2019 4:30 AM
  • User-821857111 posted

    I have added two NuGet packages of: EntityFramework, and Microsoft.EntityFrameworkCore which is my mistake
    So Patrice was right. That created a mismatch between the two versions of Entity Framework.

    Monday, November 4, 2019 9:21 PM