programmatic access to the Code First conventions code?

Topics: EF Runtime, General
Jan 22, 2013 at 8:28 PM

I've been working on trying to improve the attribute-based EF reverse engineer code templates from Rowan and Saber Soleymani here:

https://github.com/jamesmanning/EFReverseEngineerCodeFirstTemplates

Basically all the changes are in the Entity.tt

https://github.com/jamesmanning/EFReverseEngineerCodeFirstTemplates/blob/master/Entity.tt

One of the major problems I'm having is that in order to figure out what attributes do and don't need to be serialized out (Required, ForeignKey, Column, DatabaseGenerated, etc) is that I need to duplicate the logic in the Code First model building (as seen in Entity.tt)

The known bug with that Entity.tt currently is that it doesn't add the ForeignKey attribute when needed - at runtime you end up with something like "column OtherTable_Id was not found" since, even though the class in question as both the FK and the navigation property, the existing convention logic didn't "pair them up", so there needs to be an attribute to make it clear.

It seems reasonable to believe that the convention code might change over time, so rather than continuing to add more code that duplicates (or attempts to do so) the convention code already in Code First, I was wondering if there's anyway to get access to the logic via a service of some sort?

If/when such a thing could exist, I'd much rather have the reverse-generation use that to figure out which attributes were necessary (either by starting with none, and adding based on the 'model diff', or starting with all and removing the ones that don't make a difference - whatever ends up being a better approach.