从实体框架代码优先模型生成数据库脚本

编程入门 行业动态 更新时间:2024-10-25 02:28:24
本文介绍了从实体框架代码优先模型生成数据库脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是代码优先的实体框架(主要是因为使用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.

更多推荐

从实体框架代码优先模型生成数据库脚本

本文发布于:2023-07-27 12:04:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   实体   框架   模型   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!