.net 将 Entity Framework EDMX 转换为 Code First 的工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7333834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tool to convert Entity Framework EDMX to Code First
提问by SturmUndDrang
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.
是否有将 edmx 转换为代码优先的工具?我知道不久前有人谈论过 CTP 中出现过一个,但我找不到与此相关的任何更新。
There's a guy on the MSDN forumswho has written his own (not available yet), but nothing from the EF team.
MSDN 论坛上有一个人写了他自己的(尚不可用),但没有来自 EF 团队。
采纳答案by Ladislav Mrnka
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 CTP1which 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.
没有这样的工具,因为 EDMX 提供了更多无法转换为代码优先的功能。唯一可用的工具是EF Power Tools CTP1,它允许从现有数据库创建代码优先映射,但它只会创建数据库的 1:1 映像 = 基于数据库的命名、没有继承、没有拆分等。Power tools 还允许创建从现有的代码优先映射中只读 EDMX。
回答by DermFrench
With EF6 Tools & Visual Studio 2013 or Visual Studio 2012, you get the option code first from database (see screenshot below)
使用 EF6 工具和 Visual Studio 2013 或 Visual Studio 2012,您首先从数据库中获取选项代码(请参见下面的屏幕截图)


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
如果你没有看到这个选项,你需要安装 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).
这不会首先将 EDMX 转换为代码(按照相关要求),但它可以首先从现有数据库创建代码(我假设有人想从 EDMX 执行此操作,将拥有现有数据库,除非他们删除了他们的数据库) )。
回答by PilotBob
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
时代在变。有几个生成器将使用 EDMX 作为源创建代码优先实体和映射。扩展库中有几个。您最有可能想要使用的是 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.
还有 EF 5.x DbContect Generator,iirc 将映射信息放在模型上。
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.
一旦安装了其中任何一个,您就可以编辑 EDMX 并右键单击“添加代码生成项”。这将安装 .tt 模板并生成实体类、DbContext 类和映射文件。
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.
请注意,如果您有任何带有验证的部分,如果您将它们与您的 edmx 放在同一个文件夹中,并且与实体同名,它们将被覆盖。您只需要重命名这些部分,因为生成器确实将部分关键字放在实体上,或者更好的是,将该代码移动到新生成的实体类中。
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.
一旦这些文件就位,您就可以删除 .tt 文件和 .edmx 文件,然后在代码中维护您的实体。
回答by dav_i
As of 30th Jan 2013, EF Power Tools (Beta 3)is the latest version of EF Power Tools and works with EF4.2-EF6.
截至 2013 年 1 月 30 日,EF Power Tools(Beta 3)是 EF Power Tools 的最新版本,可与 EF4.2-EF6 配合使用。

