如何使用实体框架在 .NET Core 6 中播种数据?

Jer*_*Zaw 25 c# entity-framework asp.net-core-6.0

我知道如何使用我的类和创建一些初始数据的方法将数据播种到文件中旧版.NET 5.0的数据库中。startup.csSeederSeed()

public void Configure(IApplicationBuilder app, IHostingEnvironment env, Seeder seeder)
{
   seeder.Seed();

   ..............
   // other configurations
}
Run Code Online (Sandbox Code Playgroud)

如何在.NET 6.0中执行此操作?没有地方可以添加我的Seeder班级作为参数。

小智 19

我解决了类似的问题如下:

程序.cs (.NET 6)

...
builder.Services.AddScoped<IDbInitializer, DbInitializer>(); //can be placed among other "AddScoped" - above: var app = builder.Build();   

...    
SeedDatabase(); //can be placed above app.UseStaticFiles();
...    
    void SeedDatabase() //can be placed at the very bottom under app.Run()
    {
        using (var scope = app.Services.CreateScope())
        {
            var dbInitializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
            dbInitializer.Initialize();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Võ *_*Hòa 18

我以前从未使用过你的解决方案。这就是我正在做的事情

public class DataContext: DbContext
{
    public DataContext(DbContextOptions options) : base(options)
    {
        
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        new DbInitializer(modelBuilder).Seed();
    }
    
    // Db sets
}
Run Code Online (Sandbox Code Playgroud)

和

public class DbInitializer
{
    private readonly ModelBuilder modelBuilder;

    public DbInitializer(ModelBuilder modelBuilder)
    {
        this.modelBuilder = modelBuilder;
    }

    public void Seed()
    {
        modelBuilder.Entity<User>().HasData(
               new User(){ Id = 1.... },
               new User(){ Id = 2.... },

        );
    }
}

Run Code Online (Sandbox Code Playgroud)

然后运行命令

dotnet ef migrations add .......
Run Code Online (Sandbox Code Playgroud)

创建迁移文件

和

dotnet ef database update
Run Code Online (Sandbox Code Playgroud)

更新数据库


Ved*_*ant 17

.NET 6.0

使用这些链接获取详细版本:

Program.cs

var builder = WebApplication.CreateBuilder(args);

//Add services to the container.
builder.Services.AddDbContext<YourDbContext>(
    optionsBuilder => optionsBuilder.UseSqlServer("Your connection string goes here") //install - Microsoft.EntityFrameworkCore.SqlServer to use ".UseSqlServer" extension method
builder.Services.AddScoped<DbInitializer>();

var app = builder.Build();

//Configure the HTTP-request pipeline
if (app.Environment.IsDevelopment())
{
    app.UseItToSeedSqlServer();    //custom extension method to seed the DB
    //configure other services
}

app.Run();
Run Code Online (Sandbox Code Playgroud)

DbInitializerExtension.cs

internal static class DbInitializerExtension
{
    public static IApplicationBuilder UseItToSeedSqlServer(this IApplicationBuilder app)
    {
        ArgumentNullException.ThrowIfNull(app, nameof(app));

        using var scope = app.ApplicationServices.CreateScope();
        var services = scope.ServiceProvider;
        try
        {
            var context = services.GetRequiredService<YourDbContext>();
            DbInitializer.Initialize(context);
        }
        catch (Exception ex)
        {

        }

        return app;
    }
}
Run Code Online (Sandbox Code Playgroud)

DbInitializer.cs

internal class DbInitializer
{
    internal static void Initialize(YourDbContext dbContext)
    {
        ArgumentNullException.ThrowIfNull(dbContext, nameof(dbContext));
        dbContext.Database.EnsureCreated();
        if (dbContext.Users.Any()) return;

        var users = new User[]
        {
            new User{ Id = 1, Name = "Bruce Wayne" }
            //add other users
        };

        foreach(var user in users)
            dbContext.Users.Add(user);

        dbContext.SaveChanges();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我希望我可以否决评论。这个答案不是其他两个答案的重复 - 它结合了其他两个答案的各个方面,并添加了有用的重构,例如作为扩展方法的实现以及仅在开发中播种。因此,它不仅是最好的答案,而且可以说是唯一完整的答案。这个平台上的答案(如果有的话)很少符合格特评论所要求的标准。 (4认同)
  • 考虑到这个答案似乎是最完整、最容易理解的,我给他们投票。另外,这个问题并没有要求解释比较所有可能的解决方案,那么为什么当其他答案提供的信息更少时,这似乎是唯一有此要求的答案呢? (3认同)