Tool to convert Entity Framework EDMX to Code Firs

2019-01-17 14:45发布

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.

4条回答
够拽才男人
2楼-- · 2019-01-17 15:02

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.

查看更多
淡お忘
3楼-- · 2019-01-17 15:05

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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-17 15:05

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

查看更多
姐就是有狂的资本
5楼-- · 2019-01-17 15:25

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).

查看更多
登录 后发表回答