将实体框架 EDMX 转换为 Code First 的工具

发布于 2024-12-03 03:40:01 字数 267 浏览 1 评论 0原文

有没有一个工具可以将 edmx 转换为代码优先? 我知道不久前有传言称 CTP 中出现了一个,但我找不到与此相关的任何更新。

MSDN 上有一个人论坛,他自己编写了(尚不可用),但 EF 团队没有提供任何内容。

Is there a tool to convert an edmx into code-first?
I know there was talk of one appearing in a CTP a while back, but I can't find any updates relating to this.

There's a guy on the MSDN forums who has written his own (not available yet), but nothing from the EF team.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

絕版丫頭 2024-12-10 03:40:01

使用 EF6 工具和Visual Studio 2013 或 Visual Studio 2012,您首先从数据库获取选项代码(请参见下面的屏幕截图)

在此处输入图像描述

如果如果您没有看到此选项,则需要安装 Entity Framework 6 Tools for Visual Studio 2012 & 2013年
http://www.microsoft.com/en-gb/download/details.aspx? id=40762

这不会首先将 EDMX 转换为代码(按照问题中的要求),但它可以首先从现有数据库创建代码(我假设有人想从 EDMX 执行此操作,会有一个现有的数据库) DB,除非他们删除了他们的数据库)。

With EF6 Tools & Visual Studio 2013 or Visual Studio 2012, you get the option code first from database (see screenshot below)

enter image description here

If you don't see this option you need to install Entity Framework 6 Tools for Visual Studio 2012 & 2013
http://www.microsoft.com/en-gb/download/details.aspx?id=40762

This doesn't convert an EDMX to code first (as requested in question), but it can create code first from an existing DB (which I assume someone wanted to do this from EDMX, would have an existing DB, unless they deleted their DB).

撩起发的微风 2024-12-10 03:40:01

时代变了。有多个生成器将使用 EDMX 作为源来创建代码优先实体和映射。扩展库中有几个。您最可能想要使用的 C# EF 5.x DBContextFluent Generator。 http://visualstudiogallery.msdn.microsoft.com/5d663b99-ed3b-481d -b7bc-b947d2457e3c

有也是 EF 5.x DbContect Generator,iirc 将映射信息放在模型上。

一旦安装了其中任何一个,您就可以编辑 EDMX 并右键单击“添加代码生成项”。这将安装 .tt 模板并生成实体类、DbContext 类和映射文件。

请注意,如果您有任何带有验证的部分,如果您将它们放在与 edmx 相同的文件夹中且与实体同名,那么它们将被覆盖。您只需要重命名这些部分,因为生成器确实将部分关键字放在实体上,或者更好的是,将该代码移动到新生成的实体类中。

这些文件就位后,您可以删除 .tt 文件和 .edmx 文件,然后在代码中维护您的实体。

Times change. There are several generators that will create code first entities and mappings using the EDMX as the source. There are several in the extensions library. The one you will most likely want to use is the EF 5.x DBContextFluent Generator for C#. http://visualstudiogallery.msdn.microsoft.com/5d663b99-ed3b-481d-b7bc-b947d2457e3c

There is also the EF 5.x DbContect Generator which iirc puts the mapping information on the models.

Once you have installed either of these you can edit the EDMX and right click "Add Code Generation Item". This will install the .tt templates and generate the Entity Class, DbContext class and mapping files.

Be aware that if you have any partials with validation if you had them in the same folder as your edmx with the same name as the entity they would be over written. You just need to rename those partials, since the generator does put the partial keyword on the entities, or better yet, move that code into the newly generated entity classes.

Once those files are in place you can delete the .tt files and the .edmx files and going forward you maintain your entities in code.

记忆で 2024-12-10 03:40:01

There is no such tool because EDMX offers much more features which cannot be translated into code-first. The only tool available are EF Power Tools CTP1 which allow creating code-first mapping from existing database but it will only create 1:1 image of your database = naming based on database, no inheritance, no splitting, etc. Power tools also allow creating read only EDMX from existing code-first mapping.

老街孤人 2024-12-10 03:40:01

截至 2013 年 1 月 30 日,EF Power Tools (Beta 3) 是 EF Power Tools 的最新版本,适用于 EF4.2-EF6。

As of 30th Jan 2013, EF Power Tools (Beta 3) is the latest version of EF Power Tools and works with EF4.2-EF6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文