简体   繁体   English

我的 Context 没有从 Entity Framework Core 中的 DbContext 继承

[英]My Context does not inherit from DbContext in Entity Framework Core

I am trying to keep BooksContext.cs file in a folder called Contexts .我试图将BooksContext.cs文件保存在名为Contexts的文件夹中。

I have a single Book class inside Entities folder.我在实体文件夹中有一Book class 。 Hence, below is the code in BookContext.cs file.因此,下面是BookContext.cs文件中的代码。

I have used the following command at Package Manager Console to enable migrations.我在 Package 管理器控制台中使用了以下命令来启用迁移。

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

But, I'm getting below error: The type BooksContext does not inherit from DbContext .但是,我遇到以下错误: BooksContext类型不继承自DbContext The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext . DbMigrationsConfiguration.ContextType属性必须设置为从DbContext继承的类型。

Following the error, I am not sure where and how to set DbMigrationsConfiguration.ContextType property出现错误后,我不确定在哪里以及如何设置DbMigrationsConfiguration.ContextType属性

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

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.我添加了两个 NuGet 包:EntityFramework 和 Microsoft.EntityFrameworkCore,这是我的错误。

Just adding NuGet package for Microsoft.EntityFrameworkCore will do all the required work.只需为 Microsoft.EntityFrameworkCore 添加 NuGet package 即可完成所有必需的工作。

In Target Project (that contains your context) just install Microsoft.EntityFrameworkCore.Tools library and if you installed EntityFramework (not Microsoft.EntityFrameworkCore ) library, delete it.在目标项目(包含您的上下文)中,只需安装Microsoft.EntityFrameworkCore.Tools库,如果您安装了EntityFramework (不是Microsoft.EntityFrameworkCore )库,请将其删除。 then try然后尝试

enable-migrations -ContextTypeName {path of your dbcontext}

for example例如

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

it works for me in EF6.它在 EF6 中对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Entity Framework Core 中的实体获取 DbContext - Get DbContext from Entity in Entity Framework Core 在程序集中找不到上下文类型 - “ApplicationDbContext”类型不是从 DbContext 继承的 - No context type was found in the assembly - The type 'ApplicationDbContext' does not inherit from DbContext Entity Framework Core DbContext OnConfiguring 不再存在 - Entity Framework Core DbContext OnConfiguring does not exist anymore 如何刷新 Entity Framework Core DbContext 但保留我的属性装饰? - How to refresh Entity Framework Core DbContext but keep my property decorations? 实体框架DbContext如何从数据库(使用绑定)获取更新? - How does Entity Framework DbContext get updates from database (with Binding)? 在Entity Framework Core上使用using()封装DbContext - Encapsulating DbContext with using() on Entity Framework Core 无法使用 DbContext 实体框架核心 SaveChanges() - Cannot SaveChanges() with DbContext Entity Framework Core 实体框架核心 DbContext 设置 DbConnection - Entity Framework Core DbContext set DbConnection 实体框架核心 - 异步/同步 - DbContext - Entity Framework Core - Async / Sync - DbContext 实体框架核心:没有为此 dbcontext 定义无参数构造函数 - Entity Framework Core: No parameterless constructor defined for this dbcontext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM