开发者

Tool to convert Entity Framework EDMX to Code First

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.


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

Tool to convert Entity Framework EDMX to Code First

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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜