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 threexxxContactId
columns as fields - that's as expected - EF also generates three navigation properties of type
Contact
- but it calls themContact
,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 onDesignContactId
),SalesContact
(based onSalesContactId
) 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?