5

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);
        }
    }
}
6
  • Do you have a custom DbContext class which hides EF's DbContext? Commented Oct 30, 2019 at 7:21
  • No, I don't have custom DbContext class to hide EF's DbContext. All I am trying is to create BooksContext class inside Contexts folder. Commented Oct 30, 2019 at 8:35
  • Maybe I am missing something, but I cannot see an obvious mistake in that code. I would say it does inherit DbContext. Have you tried to clean and rebuild the solution? Commented Oct 30, 2019 at 8:41
  • 1
    Enable-Migration is EF6 command and is not needed in EF Core. See Entity Framework Core tools reference - Package Manager Console in Visual Studio how to install and use EF Core PMC commands. Commented Oct 30, 2019 at 9:53
  • @Fildor: Sorry for the late reply. Yes, I tried all the available alternatives, but no lock. My doubt is that: since I have created DbContext class inside Contexts folder, this problem. Commented Oct 30, 2019 at 11:03

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Not my experience. As soon as I remove EntityFramework from NuGet I start getting other errors... Your target project 'MyProject' doesn't reference EntityFramework.
You must have one reference, that is either EnitityFramework, or Microsoft.EntityFrameworkCore. Your error states that - Your target project 'MyProject' doesn't reference EntityFramework
I did have Microsoft.EntityFrameworkCore and I got errors. What I then did was to remove all EF references, and add them back making sure I had my project selected. Then it all worked. I call bugs on Microsoft.
Sorry, but I could not understand part of your statement. What do you mean by - 'I call bugs on Microsoft'?
1

In Target Project (that contains your context) just install Microsoft.EntityFrameworkCore.Tools library and if you installed EntityFramework (not Microsoft.EntityFrameworkCore) library, delete it. then try

enable-migrations -ContextTypeName {path of your dbcontext}

for example

enable-migrations -ContextTypeName CommonObjects.Entities.Data.ApplicationDbContext

it works for me in EF6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.