简体   繁体   English

如何以编程方式从ADO.Net实体模型创建数据库?

[英]How to programatically create a database from an ADO.Net Entity Model?

I am using .Net Framework 4.0. 我正在使用.Net Framework 4.0。 I have an entity model (.edmx) and I have a menu option in my program for connecting to an existing database or creating a new one. 我有一个实体模型(.edmx),并且在程序中有一个菜单选项,用于连接到现有数据库或创建新数据库。 When the user selects to create a new database, I want to generate the database and schema from the entity model. 当用户选择创建新数据库时,我想从实体模型生成数据库和架构。 Any idea how to do so? 知道怎么做吗?

Don't do it. 不要这样 It will only work on your v1 products and as soon as you'll plan to release v1.1 you'll realize you have no way of upgrading existing deployed databases to the new schema. 它仅适用于v1产品,一旦计划发布v1.1,您将意识到无法将现有已部署的数据库升级到新架构。

Instead, use scripts to create all your objects (including indexes, relations, constraints, everything) and use upgrade scripts to control how to upgrade each schema version to the next version, like described in Version Control and your Database . 而是使用脚本创建所有对象(包括索引,关系,约束,所有内容),并使用升级脚本控制如何将每个模式版本升级到下一个版本,如版本控制和数据库中所述

A weaker alternative is to have a Visual Studio Database Project in your solution because VSDB projects have upgrade capabilities, although diff based. 一种较弱的选择是在解决方案中包含Visual Studio数据库项目,因为VSDB项目具有升级功能,尽管基于差异。 They allow you to deploy .dbschema files using the vsdbcmd tool and this tool can diff and upgrade an existing schema. 它们允许您使用vsdbcmd工具部署.dbschema文件,并且该工具可以比较和升级现有架构。 You can have the VSDB project output be transformed into the EF model via an build transformation step (is not trivial, but is possible). 您可以通过构建转换步骤将VSDB项目输出转换为EF模型(这并非易事,但可以)。

Overall, do not fall for the lure of the EF (or Linq) modeling as being the master definition of your schema. 总体而言,不要将EF(或Linq)建模的诱惑视为模式的主要定义。 You'll pay the bigger price later. 稍后您将支付更高的价格。

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

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