c# - 我的上下文不是从 Entity Framework Core 中的 DbContext 继承的

标签 c# entity-framework-core-3.0

我想保持 BooksContext.cs名为 的文件夹中的文件上下文 .

我有一个 Book类内实体文件夹。因此,下面是 BookContext.cs 中的代码文件。

我在包管理器控制台中使用了以下命令来启用迁移。
PM>Enable-Migrations -ContextTypeName Books.API.Contexts.BooksContext
但是,我收到以下错误:
型号BooksContext不继承自 DbContext . DbMigrationsConfiguration.ContextType属性必须设置为继承自 DbContext 的类型.

出现错误后,我不确定在哪里以及如何设置 DbMigrationsConfiguration.ContextType属性(property)

我无法从谷歌获得太多帮助,我不确定我错过了什么。谁能帮帮我吗!

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

最佳答案

小错误,但经过一天多的痛苦努力后,对我来说是很好的学习。我希望这也能成为其他人的良好学习。

我添加了两个 NuGet 包:EntityFramework 和 Microsoft.EntityFrameworkCore,这是我的错误。

只需为 Microsoft.EntityFrameworkCore 添加 NuGet 包即可完成所有必需的工作。

关于c# - 我的上下文不是从 Entity Framework Core 中的 DbContext 继承的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619554/

相关文章:

c# - EF Core 3.0 可为空的导航属性

c# - Entity Framework Core 3.0 查询导致 "SqlException: ' Execution Timeout Expired'"和 tempdb 变满。适用于 EF Core 2.2.6

C# 等待与延续 : not quite the same?

c# - 自定义 WeakReference 实现

c# - 如何在 ASP.NET Core Web API 项目中允许使用 IP 地址进行 CORS?

c# - MVVM 按钮内容属性不起作用

c# - 进度栏帮助

c# - 如何修改基于表达式的过滤器以避免 Entity Framework Core 3.0 中的客户端评估