34

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.

Community
  • 1
  • 1
SturmUndDrang
  • 1,876
  • 5
  • 27
  • 47

4 Answers4

32

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

DermFrench
  • 3,968
  • 13
  • 43
  • 70
27

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.

PilotBob
  • 3,107
  • 7
  • 35
  • 52
  • 1
    I just tried out "EF 5.x DbContext Generator for C#", it works pretty well, except it did not port over my mappings. And the EF 5.x DBContextFluent throws an exception. – Walter Stabosz Feb 15 '13 at 21:49
  • @PilotBob can you please tell how to use it ? – Bilal Fazlani Jul 01 '13 at 11:16
  • @WalterStabosz there have been some fixes. It doesn't like a Null / Not Null discriminator on TPH, I worked around that. Also, if you see the QA and isn't always mapping FKs I had to manually add. – PilotBob Sep 10 '13 at 15:23
9

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    The Reverse Engineer to Code First facility seems like what we need. We have been updating the EDMX from our database so it should work nicely. – SturmUndDrang Sep 09 '11 at 12:46
  • 20
    "There is no such tool because EDMX offers much more features which cannot be translated into code-first" that doesn't make sense to me. At all. If you can create classes from a database, you should be able to create a similar database from those exact same classes. Sure, EF makes more stuff that can't be translated back, but they can certainly get the gist of a database. Saying otherwise just sounds fanboyish. Like "whatever they do is the best that can be done and don't question it." – vbullinger Jan 16 '13 at 14:59
  • @vbullinger: It is not about database. It about converting one mapping with significantly larger feature set into another one. There may be some tools now which will be able to achieve that for simple cases but there is plenty of advanced scenarios (especially those where you change EDMX directly without designer) which do not have any counter part in code first and thus cannot be converted - few examples: stored procedure mapping, SQL function mapping, query views, defining queries, etc. – Ladislav Mrnka Jan 16 '13 at 15:19
  • 11
    If it can't be done, @LadislavMrnka, then they're doing it wrong. Seems absurd that this can't be done. Absolutely mind-bogglingly dumb. One more time: if it CAN'T be done, then they did it incorrectly. – vbullinger Jan 16 '13 at 17:31
  • 6
    @vbullinger: No one says that those features will not become available in code first in future. It's called iterative incremental development. No company ever ships the software with all possible features in the first release. If you believe that it is so much incorrect you can always participate in making it correct - EF is open source. – Ladislav Mrnka Jan 17 '13 at 09:42
  • 1
    Good point, Ladislav. And, since there are tools that do parts of it anyway (there's something that reverse engineers code first, for example), I imagine it won't be too far away. I just feel that they did it wrong in that the code should be generated in a way that you could then use that exact code as code first. Makes no sense to do it otherwise. – vbullinger Jan 17 '13 at 15:32
  • 3
    Well as far as I can tell MSFT abandoned Model First and all efforts now go into Code First. Data Annotations, ASP.NET Identity being the two biggest missing features of Model First. – Dave May 27 '14 at 23:56
  • This answer is no longer valid - EF Tools now allows to create Code First Model from DB - https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/existing-database – Dawood Awan Sep 17 '18 at 19:19
-1

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

dav_i
  • 27,509
  • 17
  • 104
  • 136
  • 1
    Except power tools don't do what is being requested. Unless a new feature was added. – PilotBob Nov 07 '13 at 18:57
  • @PilotBob From that link: `What does it add to Visual Studio?>When right-clicking on a C# project>Reverse Engineer Code First` – dav_i Nov 07 '13 at 19:50
  • 5
    It reverses engineers the DATABASE, not your existing EDMX data model and mappings. – PilotBob Feb 14 '14 at 16:16
  • @PilotBob Though I agree with you that it doesn't reverse engineer an EDMX, it is slightly unfair that DermFrench's answer above posted over a year later re _"EF6 Tools & Visual Studio"_ has **16 up-votes** as at the time of writing and yet the tool also does not reverse engineer an EDMX. –  Dec 16 '15 at 06:26
  • Screen print yo! But, didn't your mom ever tell you, "life isn't fair"? ;) – PilotBob Dec 16 '15 at 15:15