DbContext instance cannot be used inside 'OnModelCreating' in any way that makes use of the model that is being created

huangapple go评论56阅读模式
英文:

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&lt;AppDbContext&gt; options)
        : base(options)
    {

    }

    public DbSet&lt;Book&gt; Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var _books = new List&lt;Book&gt;()
        {
           new Book()
            {
                Id = Guid.NewGuid(),
                Title=&quot;Managing Oneself&quot;,
                Description=&quot;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...&quot;,
                Author= &quot;Peter Drucker&quot;
            }
        };

        modelBuilder.Entity&lt;Book&gt;().HasData(_books);
    }
}

Program.cs

builder.Services.AddDbContext&lt;AppDbContext&gt;(opt =&gt; opt.UseInMemoryDatabase(&quot;this-is-just-test&quot;));
builder.Services.AddTransient&lt;IBookService, BookService&gt;();

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&lt;AppDbContext&gt; options)
        : base(options)
    {

    }

    public DbSet&lt;Book&gt; Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var _books = new List&lt;Book&gt;()
        {
           new Book()
            {
                Id = Guid.NewGuid(),
                Title=&quot;Managing Oneself&quot;,
                Description=&quot;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...&quot;,
                Author= &quot;Peter Drucker&quot;
            }
        };

        modelBuilder.Entity&lt;Book&gt;().HasData(_books);
    }

Program.cs

builder.Services.AddDbContext&lt;AppDbContext&gt;(opt =&gt; opt.UseInMemoryDatabase(&quot;this-is-just-test&quot;));
builder.Services.AddTransient&lt;IBookService, BookService&gt;();

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&amp;tabs=visual-studio#seed-the-database

That way it executes after the table is created...

huangapple
  • 本文由 发表于 2023年2月16日 02:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定