简体   繁体   English

EFCore 框架脚手架覆盖 DBContext 文件

[英]EFCore Framework Scaffolding overwrites DBContext file

I am trying to leverage scaffolding for EntityFrameworkCore to create an entity framework models for existing Sql Server database.我正在尝试利用 EntityFrameworkCore 的脚手架为现有的 Sql Server 数据库创建实体框架模型。 This works fine and it did create the context file and all the tables.这工作正常,它确实创建了上下文文件和所有表。

However going forward I still want to continue to update the DB and get the model updated based on Database changes.然而,我仍然希望继续更新数据库并根据数据库更改更新模型。 This works fine with force flag on Scaffolding as it overwrites the tables with new changes.这适用于脚手架上的 force 标志,因为它会用新的更改覆盖表。

The main concern I have is scaffolding needs db connection string which gets included in the DBContext file.我主要关心的是脚手架需要包含在 DBContext 文件中的数据库连接字符串。 And on running scaffolding command this file gets overwritten as well.在运行脚手架命令时,这个文件也会被覆盖。 Ideally i would like to save it in config file and have different value based on environments.理想情况下,我想将其保存在配置文件中,并根据环境具有不同的值。 However this means every time model is updated all my config changes are lost and I need to manually reapply the same然而,这意味着每次更新模型时,我的所有配置更改都会丢失,我需要手动重新应用相同的

Is there any efficient way to do the same?有没有什么有效的方法可以做到这一点?

使用 EF Core Power Tools,并使用带有 DbContextOptionsBuilder 的构造函数,并在 DbContext 类之外指定特权和相应的连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM