Reputation: 21314
I am a bit confused on what is happening with EF5 in VS.NET 2012 when I do a database first approach that creates a .edmx file. The reason I'm confused is because there is such a plethora of information on EF 4.x that a lot of the information is inaccurate in relation to EF5 I believe.
In EF4 to use POCOs with a database first approach, one would create the POCO classes, and make sure to set Code Generation Strategy = None. Then create a separate say 'Entites' class that inherits from `ObjectContext' that has the knowledge of our POCO classes for use with EF.
In EF5 with VS.NET 2012, when I do a database first approach, Code Generation Strategy = None is already set, and the resulting classes generated by the default T4 template, seem to already create POCO classes for me. The resulting classes have no inheritance on ObjectContext
or DBContext
. Is this how the auto-generated entities are created by default now as POCO classes?
If the answer is 'Yes', I actually like that a lot. My main question would be, can I yank those POCO classes out into another layer? Right now they are shown under 'MyModel.tt', so if I remove them I suppose any changes would not be reflected if I update the model, correct?
Thanks!
Upvotes: 4
Views: 2328
Reputation: 364399
In EF4 to use POCOs with a database first approach, one would create the POCO classes, and make sure to set Code Generation Strategy = None. Then create a separate say 'Entites' class that inherits from `ObjectContext' that has the knowledge of our POCO classes for use with EF.
That was a core idea but you could also use additional T4 templates downloaded for example from VS Gallery which would generate POCO classes for you.
In EF5 with VS.NET 2012, when I do a database first approach, Code Generation Strategy = None is already set, and the resulting classes generated by the default T4 template, seem to already create POCO classes for me. The resulting classes have no inheritance on ObjectContext or DBContext. Is this how the auto-generated entities are created by default now as POCO classes?
Yes. VS 2012 by default uses T4 templates for generation POCO classes from model.
can I yank those POCO classes out into another layer? Right now they are shown under 'MyModel.tt', so if I remove them I suppose any changes would not be reflected if I update the model, correct?
Yes you can with some limitations. You can move whole .tt file to another folder or project and you just need to update the path in this file to point to correct location of EDMX file. The .tt file is T4 template responsible for generating your POCO classes. The main limitation may be autoupdating - with default configuration the template is updated and saved automatically when your EDMX file is saved. Saving the template will trigger regeneration of all POCO classes (= another limitation - do not modify those autogenerated classes). When you move the template to another project this automagic does not work and you must manually trigger Run custom tool from context menu on .tt file to force class regeneration.
Upvotes: 3