我的上下文未从Entity Framework Core中的DbContext继承 [英] My Context does not inherit from DbContext in Entity Framework Core
问题描述
我正在尝试将BooksContext.cs
文件保存在名为 Contexts 的文件夹中.
I am trying to keep BooksContext.cs
file in a folder called Contexts.
我在实体文件夹中只有一个Book
类.因此,下面是BookContext.cs
文件中的代码.
I have a single Book
class inside Entities folder. Hence, below is the code in BookContext.cs
file.
我已经在Package Manager控制台中使用以下命令来启用迁移.
I have used the following command at Package Manager Console to enable migrations.
PM>Enable-Migrations -ContextTypeName Books.API.Contexts.BooksContext
但是,我遇到了以下错误:
类型BooksContext
不能从DbContext
继承. DbMigrationsConfiguration.ContextType
属性必须设置为继承自DbContext
的类型.
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
.
出现错误后,我不确定在何处以及如何设置DbMigrationsConfiguration.ContextType
属性
Following the error, I am not sure where and how to set DbMigrationsConfiguration.ContextType
property
我无法从Google那里获得太多帮助,而且我不确定自己缺少什么.谁能帮我!
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.
我添加了两个NuGet软件包:EntityFramework和Microsoft.EntityFrameworkCore,这是我的错.
I have added two NuGet packages of: EntityFramework, and Microsoft.EntityFrameworkCore which is my mistake.
只需为Microsoft添加NuGet包,EntityFrameworkCore即可完成所有必需的工作.
Just adding NuGet package for Microsoft.EntityFrameworkCore will do all the required work.
这篇关于我的上下文未从Entity Framework Core中的DbContext继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!