Generating Entity Framework "POCO" classes

Issue #228 resolved
Brian Lewis repo owner created an issue

Key differences in moving to Entity Framework from Linq2Sql:

L2S use the custom dbml designer to build its model of the database. EF uses explicit, and simpler, classes:

  • context class - the overarching data "context" PineapplesEFContext.
  • entity classes - one class for each entity , correspond to database tables basically. These classes are now in Pineapples.Data.Models; e.g. Books.cs

The context class contains a collection property for each entity e.g. cxt.Books

While called 'POCO' classes (= 'plain old c# objects') they are not really that 'POCO' : they are decorated with lots of attributes used by EF when accessing this data.

How to generate these classes - now we don;t have the Linq@sql designer any more?

I've settled on this tool:

(https://www.codeproject.com/Articles/892233/POCO-Generator)

It generates everything we need it to do, its failing is that it can be quite slow on a big database (parsing the entire database, including all stored procs, on load) . However, it has a command line mode which is probably all we'll ever need.

There is a Visual Studio addin that works with the templating infrastructure (TT) - this can require you to edit the TT files, and I think this will make it harder to work concurrently on different items, or manually tweak things. the first option is just.... simpler, if not quite as slick.

Please download the POCO generator, have a play with it; I have been using these options:

Capture.PNG

The equivalent command line is:

Capture.PNG

Binders

Note that the "Binder" architecture is relatively the same as before; there are two implementations of ModelBinder - for L2S and for EF - i have changed all the Pineapples binders to be derived now from the EF model binder. Only change is there is now an Update method - rather than SubmitChanges, which is very much as L2S term.

The Binders also make use of some custom Attributes I have added "ChangeTrackingAttribute" which identifies the create/edit user and datetime. The Binder updates all these as appropriate when inserting or updating a record.

To get all the pCreateUser, pEditDateTime etc fields, correctly attributed, edit your POCO class to derive from ChangeTracked.

Comments (2)

  1. Log in to comment