从实体框架代码优先模型生成数据库脚本 [英] Generating a database script from entity framework code-first model

查看:87
本文介绍了从实体框架代码优先模型生成数据库脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是代码优先的实体框架(主要是因为使用RIA Services,无论如何,您都需要某种形式的代码类以获取其他属性)。

I'm using entity framework with code-first (primarily because with RIA Services, you need to have some form of the code classes anyway for additional attributes).

我想要一个数据库生成脚本来轻松更新Visual Studio数据库项目。我不想使用ef迁移,我更喜欢数据库项目。

I want to get a database generation script to easily update the visual studio database project. I don't want to use ef migrations, I prefer the database projects.

我可以使用类似

string sqlscript = (context as IObjectContextAdapter).ObjectContext.CreateDatabaseScript();

在运行时,但是我想有一个便捷的方法来获取脚本而无需在某些情况下运行项目

at runtime, but I want to have a convenient method to get the script without running the project in some way, and without using migrations.

有人有任何想法吗?

我正在使用ef4.1和ef5在不同的项目中。

I'm using ef4.1 and ef5 in different projects.

[编辑:回答此问题的一种方法是从程序包管理器控制台中调用上述代码。]

[ One way of answering this question would be a way to call above line from the package manager console.]

推荐答案

一种方法是使用Entity Framework Power Tools。他们为包含DbContext类的文件添加一个上下文菜单项,该类为数据库生成DDL文件。

One way is using the Entity Framework Power Tools. They add a context menu entry for the file containing the DbContext class which generates a DDL file for the database.

另一种方法是使用LinqPad。在LinqPad中打开dll后,可以在查询中执行问题中给出的代码段:

Another way is using LinqPad. After opening the dll in LinqPad, one can execute the code snippet given in the question in a query:

(this as IObjectContextAdapter).ObjectContext.CreateDatabaseScript()

尽管这两种方式都需要第三方工具,但这两种方式都有些不便。

Both ways are slightly inconvenient though as they require third-party tools.

这篇关于从实体框架代码优先模型生成数据库脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆