2

The way I handle large projects using EF CF is by doing the following:

  • Create a data model (EDMX)
  • Disable the Model-First code generation feature for that model by clearing the Custom Tool property
  • Use Entity Framework POCO Generator extension to add the relevant template (t4) files to the project
  • Customize the template files to follow certain coding conventions we follow

This all looks good in concept but when we run the project, EF seems to think we are using the Model-First approach. This is not the case since we do not want the EDMX model mapped to any database. We expect the database to be created at runtime if the model has changed.

So how can we remove database mapping expectations of the EDMX?

1 Answer 1

4

By not using EDMX. EDMX is the mapping. Once you use EDMX you are using either Model-first or Database-first. Code first means no EDMX and no code generators. The code in the approach name means that you are supposed to write that code. The only way to get entities and mapping generated in code first is when you have an existing database and you use EF Power Tools but if you don't have the database you don't have any such support.

16
  • Sounds like he has a database so can just use the EF Power Tools. It's a really easy process and you can tweak from there. Apr 30, 2012 at 14:50
  • I have to differ. Code generation is very much possible in Code-First. I myself have been writing code generators for a while and conceptually you should be able to generate entity-aware POCOs from your own classes. But in orer to avoid writing my VS extension, I have created a separate project in my solution, added an EDMX and custom templates, generated code, copied it into my master project as partial classes. Apr 30, 2012 at 14:58
  • 1
    Yes you can use code generators, but in terms of EF you cannot use code generators from EDMX - at least not default ones because EDMX is not just a diagram. It is a mapping and you need to get that mapping out of EDMX to the code if you don't want to use EDMX at runtime - those default generators are not doing this. Apr 30, 2012 at 15:05
  • @RaheelKhan - you can beg to differ all you like, that doesn't change the fact you are NOT using code first. You're using Database First with Poco class generators. This still uses the EDMX as the model. If you want code first, then you need to either write it from scratch as code-first, or use EF Power Tools. Apr 30, 2012 at 15:17
  • @MystereMan: Please understand the following: I AM using an EDMX Model. I AM NOT using a database. I am using the EDMX to generate Code First classes, NOT Model First classes. The reason people prefer to do that is to have your classes more persistence agnostic than Model or Database First. Apr 30, 2012 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.