[英]How can I collect the current SQL Server Session ID from an Entity Framework DbContext?
[英]How can I create the SQL Server database scheme for a Docker image based on an existing DbContext with Entity Framework?
Started working with Docker images recently, and my main problem is that while my docker-compose
can spin up the api, and the database I need, what it lacks is the actual scheme of the database that the entities within my API project look for when getting data .
所以我已經有一個DbContext
,所有的實體,我就是無法弄清楚如何根據我的 API 項目中已經存在的上下文在 SQL 服務器映像中構建數據庫。
docker-compose
文件供參考:
networks:
event_localhost:
services:
api:
build:
context: ..
dockerfile: .docker/Dockerfile
image: spc-eventapi
depends_on:
- db
ports:
- 8000:80
networks:
- event_localhost
environment:
ConnectionStrings__EventDb: 'Server=db,1433;Database=master;User Id=sa;Password=Pass@word;'
db:
image: mcr.microsoft.com/mssql/server
networks:
- event_localhost
environment:
SA_PASSWORD: 'Pass@word'
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
version: '3.9'
有效的修復是使用實體框架允許的功能,being.EnsureCreated()
將以下代碼行添加到 Startup 中的 Configure 方法會在容器中創建架構和數據庫(如果尚不存在)
// Ensures DB is created against container
IServiceScopeFactory serviceScopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
using IServiceScope serviceScope = serviceScopeFactory.CreateScope();
SantaPoneCentralDatabaseContext dbContext = serviceScope.ServiceProvider.GetService<SantaPoneCentralDatabaseContext>();
dbContext.Database.EnsureCreated();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.