Я пытаюсь сохранить файл BooksContext.cs
в папке с именем Контексты .
У меня есть один класс Book
внутри папки Entities . Следовательно, ниже приведен код в файле BookContext.cs
.
Я использовал следующую команду на консоли диспетчера пакетов для включения миграции.
PM>Enable-Migrations -ContextTypeName Books.API.Contexts.BooksContext
Но яполучаю ошибку ниже: тип BooksContext
не наследуется от DbContext
. Свойство DbMigrationsConfiguration.ContextType
должно быть установлено на тип, который наследуется от DbContext
.
После ошибки я не уверен, где и как установить DbMigrationsConfiguration.ContextType
свойство
Я не могGoogle не очень помогает, и я не уверен, что мне не хватает. Может кто-нибудь, пожалуйста, помогите мне!
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);
}
}
}