简体   繁体   English

如何基于具有实体框架的现有 DbContext 为 Docker 图像创建 SQL 服务器数据库方案?

[英]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. 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 .

So I have a DbContext already, all the entities, and I namely cant figure out for the life of me how to build the database in the SQL Server image based on the context that is already in my API project.所以我已经有一个DbContext ,所有的实体,我就是无法弄清楚如何根据我的 API 项目中已经存在的上下文在 SQL 服务器映像中构建数据库。

docker-compose file for reference: 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'

Context and entities: https://github.com/SantaPoneCentralDev/santaponecentral/tree/Docker/SPC-2021/api/event/event/Event.Data/Entities上下文和实体: https://github.com/SantaPoneCentralDev/santaPonecentral/tree/Docker/SPC-2021/api/event/event/Event.Data/Entities

The fix that worked was using the functions that entity framework allows, being.EnsureCreated()有效的修复是使用实体框架允许的功能,being.EnsureCreated()

Adding the following lines of code to the Configure method in Startup creates the schema and DB in the container if it doesn't already exist将以下代码行添加到 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.

相关问题 如何从实体框架DbContext收集当前的SQL Server会话ID? - How can I collect the current SQL Server Session ID from an Entity Framework DbContext? 如何使用Entity Framework在现有SQL Server数据库中创建表? - How to use Entity Framework to just create a table in an existing SQL Server database? 如何在实体框架核心中创建DBContext作为现有事务的一部分 - How to create a DBContext as part of an existing transaction in Entity Framework Core 实体框架更改现有DbContext的数据库 - Entity Framework changing database of existing DbContext 如何在DbContext之外使用Entity Framework之前创建数据库? - How to make Entity Framework create database before using it outside DbContext? 如何使用 Entity Framework 6 在内存中创建 DbContext? - How do I create an in memory DbContext with Entity Framework 6? 如何首先在特定路径中通过实体框架代码创建 sql 数据库 - how can i create sql database by entity framework code first in specific path 如何创建实体框架 DBContext 扩展方法? - How to Create Entity Framework DBContext Extension Method? 如何使用Entity Framework 6在运行时创建数据库和表? - How can I create a database and tables at runtime using Entity Framework 6? 使用Entity Framework Code First开发,我能否在现有数据库中创建一个或多个表? - Using Entity Framework Code First Development, can I get it to create a table or tables in an existing database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM