英文:
Containerized .NET Api, Database.Migrate() not creating migrations
问题
抱歉,你的请求不清楚。请提供需要翻译的具体内容。
英文:
Sorry if the question might seem stupid, I am still learning docker, containers and .NET. I have a SQL Server db configured in a docker container with a .NET Web Api in another container, both started with a docker-compose. I configured my database to perform automatic migrations. My problem is that migrations are not created whenever I do changes in any entity. I also tried doing update-database
manually, but I cannot since I am starting docker-compose using Visual Studio and apparently the Startup project needs to be an Api (Users.Api in my case). If I set Users.Api as the Startup project, I do not have a running database container, so the operation cannot connect to the db.
Snippet of docker-compose
usersdb:
container_name: usersdb
restart: always
user: root
environment:
SA_PASSWORD: aCertainPassword
ACCEPT_EULA: Y
ports:
- "1433:1433"
volumes:
- users_data:/var/opt/mssql/data
users.api:
container_name: users.api
environment:
- ASPNETCORE_ENVIRONMENT=Development
depends_on:
- usersdb
ports:
- "8300:80"
And here I have the Program.cs
using Microsoft.EntityFrameworkCore;
using Users.Infrastructure.Extensions;
using Users.Infrastucture.Contexts;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper();
builder.Services.AddRepositories();
builder.Services.AddDbContext<UsersContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
using (var scope = app.Services.CreateScope())
{
var dataContext = scope.ServiceProvider.GetRequiredService<UsersContext>();
dataContext.Database.Migrate();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
答案1
得分: 2
一种解决方法是只需以分离模式启动我的容器,并通过Visual Studio手动添加迁移。我不会将这标记为正确答案,因为可能还有其他更好的方法来完成这个任务。
英文:
One work around for this was to just start my containers in detached mode and add the migrations manually myself through Visual Studio. I will not mark this as the right answer since maybe somebody else has a better way to do it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论