简体   繁体   English

以编程方式从EDMX生成DDL

[英]Generating DDL from EDMX programmatically

I want to create a deployment page in my web application. 我想在Web应用程序中创建一个部署页面。 I would also like to use the same method to create an upgrade page for the database. 我还想使用相同的方法为数据库创建一个升级页面。 I use the following code to generate the DDL sql scripts for the EDMX file i have. 我使用以下代码为我拥有的EDMX文件生成DDL sql脚本。

IObjectContextAdapter context = new ApplicationDBContext();
var sqlScript = context.ObjectContext.CreateDatabaseScript();

this generates a creation script for the entire database. 这将为整个数据库生成一个创建脚本。

I would like it to create a DDL script that will use IF EXSISTS and DROP CREATE decleartion to reset the existing database. 我希望它创建一个DDL脚本,该脚本将使用IF EXSISTSDROP CREATE清除来重置现有数据库。

is it possible ? 可能吗 ?

I would also like to have a toggle that will upgrade the current database only adding the new fields and tables that does not exist. 我还希望有一个切换将升级当前数据库,仅添加不存在的新字段和表。

is it also possible ? 还有可能吗?

Checkout context.Database property. 检出 context.Database属性。 You will find a lot of useful methods in it. 您会在其中找到许多有用的方法。 You basically can check if database exists, drop, create database, check if it is compatible with your model. 您基本上可以检查数据库是否存在,删除,创建数据库,检查它是否与您的模型兼容。

Also consider Entity Framework Migrations. 考虑实体框架迁移。 This is a modern way to handle database changes. 这是处理数据库更改的现代方法。

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

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