8

We've been using EF 4.0 with a database-first approach using an .edmx model.

We're now upgrading to EF 6.1.3, and we're investigating whether to go with or without that .edmx model. We definitely still want to be in control of our database schema - so we'll be creating new tables etc. with SQL script and then generating the EF model (either .edmx or just code classes) from the existing database.

During my trials with Visual Studio 2013, I noticed an annoying point when EF 6 creates the classes from the existing database. I have a Customer class which links to a Contact class three times - once for the DesignContact, once for the SalesContact and a third time for the SupportContact. Those are stored as DesignContactId (INT) etc. columns in the Customer table.

You can create those classes using this T-SQL:

CREATE TABLE dbo.Contact
(
    ContactID INT NOT NULL 
        CONSTRAINT PK_Contact PRIMARY KEY CLUSTERED,
    Name VARCHAR(200),
    Address VARCHAR(200),
    ZipCode VARCHAR(20),
    City VARCHAR(100),
    Phone VARCHAR(100)
)

CREATE TABLE dbo.Customer
(
    CustomerID INT NOT NULL 
        CONSTRAINT PK_Customer PRIMARY KEY CLUSTERED,
    Name VARCHAR(200),
    -- other properties
    DesignContactID INT 
        CONSTRAINT FK_Customer_DesignContact
            FOREIGN KEY REFERENCES dbo.Contact(ContactID),
    SalesContactID INT 
        CONSTRAINT FK_Customer_SalesContact
            FOREIGN KEY REFERENCES dbo.Contact(ContactID),
    SupportContactID INT 
        CONSTRAINT FK_Customer_SupportContact
            FOREIGN KEY REFERENCES dbo.Contact(ContactID),
)

When creating the classes from the database, I see this:

  • the Customer class has my three xxxContactId columns as fields - that's as expected
  • EF also generates three navigation properties of type Contact - but it calls them Contact, Contact1, Contact2 - uuuuuggghHHHH!

I'm trying to find a way to get around those horribly bad named navigation properties..... these are bad because:

  • their not intuitive - which one points to which contact now?
  • are the "stable", or could they change over time, if a fourth relationship to Contact is introduced? Not sure..... (can't find any docs on this)
  • just their names are absolutely horrible...... why aren't they called DesignContact (based on DesignContactId), SalesContact (based on SalesContactId) etc.?

In EF 4.1 and up, you were able to add T4 templates to the .edmx and influence the generation process - is that still possible somehow with EF6, if you're doing "Generate code-first from existing database" approach? I couldn't find any blog post or article on that topic anymore.....

Or is there another way to influence how EF generates the classes - most notably the navigation properties? Can I define some kind of custom convention or something to influence this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    @JulieLerman: any thoughts on this (as the _Queen of EF_ ? :-) ) – marc_s Sep 11 '15 at 11:46
  • @RowanMiller: any ideas or tricks up your sleeve to handle this? – marc_s Sep 11 '15 at 11:47
  • Applicable: http://stackoverflow.com/a/13064383/1207195? (not voting as dupe because of hammer) – Adriano Repetti Sep 11 '15 at 11:55
  • @AdrianoRepetti: NO, that's not applicable - that other question uses the EF Power Tools which is **NO LONGER** available (it seems) for EF 6.1.3 and VS 2013 ..... so I cannot apply that solution to my problem. – marc_s Sep 11 '15 at 11:57
  • 1
    aren't PT available in EF6?! https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/ – Adriano Repetti Sep 11 '15 at 12:03
  • @AdrianoRepetti: hmm..... let me check ....... – marc_s Sep 11 '15 at 12:04
  • 1
    I always use the beta version of [Power Tools](https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d) and it generates ok. Then add T4 templates inside VS. Only problem is you have to generate all objects which can take an age. – Lee.Winter Sep 11 '15 at 12:21
  • 1
    Don't know if that helps, but have you seen https://efreversepoco.codeplex.com/ – M4N Sep 11 '15 at 13:59
  • @M4N: EF Reverse Poco Generator looks quite impressive indeed ! Thanks for the link – marc_s Sep 11 '15 at 20:53
  • @AdrianoRepetti: OK, the EF Power Tools still work just fine even in VS2015 and with EF 6 - now the challenge is understanding the object model so that I can figure out how to name my navigation properties the way I'd like them to be :-) – marc_s Sep 16 '15 at 05:25

2 Answers2

0

You can change the settings for generated classes in the .edmx file (via notepad, etc). Just back it up before you change it. It's not ideal, but it works and updates from the db don't overwrite your changes.

<EntityType Name="SomeEntityName" a:TypeAccess="Internal" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration">
    <Key>
        <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
    <Property Name="Contact" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="false" />
    <Property Name="DesignContact" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="false" />
</EntityType>
Tony Karel
  • 176
  • 1
  • 10
  • I **don't have** an `.edmx` file...... this is generated from the database into **code-first** (or code-only) C# classes - no more `.edmx` file..... – marc_s Oct 06 '15 at 20:19
  • @marc_s Poor reading on my part. I've gone code-first and writing the entity mapping isn't too bad, which, if I'm reading correctly, would alleviate your issue. Otherwise I've generated the edmx and edited it if needed. – Tony Karel Oct 06 '15 at 20:30
0

Thanks to everyone for chipping in - I tried various approaches:

  • I tried the "EF Reverse Poco" project on Codeplex but wasn't quite happy with it at all

  • I tried the EF Power Tools for EF 6 - and it worked just fine. This allowed me to add T4 templates to influence the code generation process. Works - but the challenge is now to understand the object model being used as the basis for the code generation (which seems to be undocumented....)

  • I tried the Entity Framework 6.1.3 Code Templates for C# NuGet package - works too, quite nicely, but has the same issues as the Power Tools - to actually change anything, a lot of digging into the object model will be needed

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459