14

I'm in the process of converting an extensive EDMX model into POCO classes. I need to go from a Database First approach (EDMX with ObjectContext) to a pure Model First approach (DbContext with no EDMX file). I need to use the latest Entity Framework stable version: 6.1.1.

I've tested some approaches:

  • Adding a the EF 6.x DbContext Generator code generation item by right-clicking the blank space in EDMX designer. This works fine, but it doesn't add any mappings. With this approach I have to still use the EDMX file. It's not full Code First.
  • Using the EF 5.x DbContext Fluent Generator for C#. This triggers an exception in design time. I'm not being able to use it. I don't know if that's because my VS Entity Framework tools are already updated to 6.x. Using the alternative TT in the comments, that suggests that it would work with EF 6.0 also doesn't help.
  • Using the EntityFramework Reverse POCO Generator. This is the worst because it won't consider any of my classes and navigation properties renames.
  • Using the Entity Framework Power Tools Beta 4. Again, it only supports generating from the database, not from the EDMX file.

My requirements:

  • I need the input to be the EDMX file, not the database.
  • I need the output to be a full Code First approach with Fluent mappings.
  • I need all my navigation property names defined in the EDMX to be considered because otherwise it would break a large codebase, even more then migrating from ObjectContext to DbContext will break.

What do you think would be a good option for me to go?

Andre Pena
  • 56,650
  • 48
  • 196
  • 243
  • 2
    Entity Framework power tools extension in visual studio, https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d – Hamid Pourjam Dec 10 '14 at 16:15
  • Thank you @dotctor , but Entity Framework Power Tools does not support generating POCO and Mappings from an EDMX file, only from the database server. – Andre Pena Dec 10 '14 at 20:39
  • you can generate a dummy database from your edmx and then reverse engineer it to generate your pocos – Hamid Pourjam Dec 10 '14 at 20:40
  • 1
    @dotctor, I could not, because the reverse navigation property names cannot be infered from the database. Trust me, the model is huge. But I appreciate you tried to help. – Andre Pena Dec 10 '14 at 20:41
  • I'm pretty sure I've seen this question before and that the answer has always been "no". An obvious first step would be to migrate to `DbContext` first. This will probably cause you a couple of headaches, but then you have all the benefits of the better API. As a next step you could consider introducing bounded code-first contexts that gradually take over parts of the old context. When the model is "huge", having several smaller contexts may be a good idea anyway. – Gert Arnold Dec 10 '14 at 21:01
  • Did you ever get anywhere with this? I'm in a similar situation. I was planning to progress in two steps, dbcontext first, then fluent API when I was happy, but my edmx now gives an error on model load, which it doesn't for ObjectContext. I think my next step might be to write my own tt file, but if I can avoid doing that... – Kevin O'Donovan Feb 07 '19 at 11:43
  • Still no way to do this in 2021 ? I would need to migrate a VERY big EDMX file into C# fluent mapping, then later, separate it into multiple contexts... I am not very happy to discover there is no simple way to to it ! ;-) – Aloene Feb 06 '21 at 11:16

2 Answers2

1

Well i don't think there is an easy one click solution to this.

Underneath you edmx files. You have two more files available besides the xx.Designer.cs and xx.edmx.diagram.. called xx.Context.tt and xx.tt where xx is the name of your edmx model.

These are t4 templates which genrate your dbcontext and poco objects. All your poco objects would be created underneath your xx.tt files and dbcontext underneath your xx.Context.tt files.

You now have to moves these into separate files. This is much easier if you are using EF6. and the file generated are already using DbContext and not ObjectContext.

amuz
  • 334
  • 2
  • 11
  • Thank you, but then I'd still need the EDMX and I wouldn't have the fluent mappings. I need to get rid of the EDMX and have the mappings all done by code. – Andre Pena Dec 11 '14 at 00:35
  • Well, if you copy the generated .cs files from underneath the tt templates.. you do not need your edmx anymore.. and the fluent mapping are already present in your generated context files.. – amuz Dec 11 '14 at 00:40
  • They are not there.. the mappings are not translated into C# files. They are still kept in the EDMX file and that's why the connection string is an EF one, and not a standard SQL Server one. That's because the actual mappings are still in the EDMX file. – Andre Pena Dec 11 '14 at 00:47
  • Check what you code generation strategy is set in your edmx file. More information here http://msdn.microsoft.com/en-us/library/vstudio/cc982042(v=vs.100).aspx . – amuz Dec 11 '14 at 00:53
  • It's T4 and it generates everything besides the fluent mappings. PS: I'm using the default approach(#1 in my list). Thank you anyway @amuz, and welcome to SO. – Andre Pena Dec 11 '14 at 00:56
0

I faced a similar case and I used Entities to DTO's generator. Although its purpose is to generate DTO's, however, I believe it can help someone in you case.

https://entitiestodtos.codeplex.com/

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54