英文:
DbContext instance cannot be used inside 'OnModelCreating' in any way that makes use of the model that is being created
问题
以下是您要翻译的部分:
Error:
EntityType '((Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TestApp_40_xUnit_MVC_LibraryApp.Models.Book>)this.Books).EntityType' threw an exception of type 'System.InvalidOperationException' Microsoft.EntityFrameworkCore.Metadata.IEntityType {System.InvalidOperationException}
Message An attempt was made to use the model while it was being created. A DbContext instance cannot be used inside 'OnModelCreating' in any way that makes use of the model that is being created.
getting an error on line:
protected override void OnModelCreating(ModelBuilder modelBuilder)
DbContext file
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var _books = new List<Book>()
{
new Book()
{
Id = Guid.NewGuid(),
Title="Managing Oneself",
Description="We live in an age of unprecedented opportunity: with ambition, drive, and talent, you can rise to the top of your chosen profession, regardless of where you started out...",
Author= "Peter Drucker"
}
};
modelBuilder.Entity<Book>().HasData(_books);
}
}
Program.cs
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase("this-is-just-test"));
builder.Services.AddTransient<IBookService, BookService>();
Model class
public class Book
{
public Guid Id { get; set; }
[Required]
public string Author { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
}
英文:
Detail: I am trying to seed some test data before my application start. I am using .net6
& following link
Error:
> EntityType '((Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TestApp_40_xUnit_MVC_LibraryApp.Models.Book>)this.Books).EntityType'
> threw an exception of type
> 'System.InvalidOperationException' Microsoft.EntityFrameworkCore.Metadata.IEntityType
> {System.InvalidOperationException}
>
> Message An attempt was made to use the model while it was being created. A
> DbContext instance cannot be used inside 'OnModelCreating' in any way
> that makes use of the model that is being created.
getting an error on line:
> protected override void OnModelCreating(ModelBuilder modelBuilder)
debug: even with error, application runs file but no test data is inserted
DbContext file
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var _books = new List<Book>()
{
new Book()
{
Id = Guid.NewGuid(),
Title="Managing Oneself",
Description="We live in an age of unprecedented opportunity: with ambition, drive, and talent, you can rise to the top of your chosen profession, regardless of where you started out...",
Author= "Peter Drucker"
}
};
modelBuilder.Entity<Book>().HasData(_books);
}
Program.cs
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase("this-is-just-test"));
builder.Services.AddTransient<IBookService, BookService>();
Model class
public class Book
{
public Guid Id { get; set; }
[Required]
public string Author { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
}
答案1
得分: 1
将种子函数的调用放在 "program.cs" 中。可以参考这个示例:https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-7.0&tabs=visual-studio#seed-the-database
这样它会在表创建之后执行...
英文:
I would put the call to the seed function in "program.cs". See this as an example: https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-7.0&tabs=visual-studio#seed-the-database
That way it executes after the table is created...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论