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
使用这些链接获取详细版本:
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)