gpt4 book ai didi

entity-framework - EntityFramework Core 自动迁移

转载 作者:行者123 更新时间:2023-12-03 08:44:15 26 4
gpt4 key购买 nike

Entity Framework core中是否有执行自动迁移的代码code first在 asp.net 核心项目中?

我只是在 MVC4/5 中添加

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, MyProject.Migrations.Configuration>());
public Configuration() {
AutomaticMigrationsEnabled = true;
}

这可以在实体更改时节省时间

最佳答案

这就是他们在 IdentityServer4 http://identityserver.io 中的做法

public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// this will do the initial DB population
InitializeDatabase(app);
}

private void InitializeDatabase(IApplicationBuilder app)
{
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
...
}
}

关于entity-framework - EntityFramework Core 自动迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com