简体   繁体   English

如何从实体框架dbcontext中制作SQL脚本文件

[英]how to make sql script file from entity framework dbcontext

is is possible to generate sql script from dbcontext of entity framework to create the whole database or at least tables with previously created database via ssms . 是有可能产生从实体框架的DbContext SQL脚本创建整个数据库或者经由SSMS先前创建的数据库至少表。

So when my dbcontext and models are done, can i make sql script witch will be able to recreate the whole db (it can be empty, it doesn't matter). 因此,当我完成dbcontext和模型后,我可以使sql脚本witch能够重新创建整个db吗(它可以为空,没关系)。

Now, i know that migrations do that but the problem is that i have many migrations so the code for sql is scattered all over the place. 现在,我知道迁移可以做到这一点,但问题是我有很多迁移,因此sql的代码散布在各处。

So, is it possible to make that sql script when I am finished modeling db from code first approach? 因此,当我从代码优先方法完成db建模后,是否可以制作该sql脚本?

To do this run the Update-Database command in the Package Manager Console but this time specify the –Script flag so that changes are written to a script rather than applied. 为此,请在程序包管理器控制台中运行Update-Database命令,但这一次指定–Script标志,以便将更改写入脚本而不是应用。 [source] [资源]

update-database -script

I would let EF create DB and than using SSMS you can generate scripts even with data. 我会让EF创建数据库,而不是使用SSMS甚至可以使用数据生成脚本。 Right click db in SSMS > Tasks > Generate Scripts and then in dialog window where they want you to select file for saving SQL script choose Advanced . 右键单击SSMS>任务>生成脚本中的db,然后在对话框窗口中,他们希望您选择用于保存SQL脚本的文件,请选择“ 高级” There you can select what you want to generate. 您可以在此处选择要生成的内容。

在此处输入图片说明

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

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