0

Is there a way to set entity reference names from the database?

  1. I’m using database first.
  2. I have a table with multiple foreign keys to the same table (Address) as shown below.
  3. EF creates entity relations for the 2 addresses and sets the names to AddressReference and Address1Reference which is not as descriptive as I would like.

I know I can do this from the EF side with annotations but is there a way to set the entity reference names from the database?

CREATE TABLE [dbo].[Person] (
    [ID] INT PRIMARY KEY IDENTITY NOT NULL,
    [Name] NVARCHAR(50) NOT NULL,
    [HomeAddressID] INT,
    [WorkAddressID] INT
)
GO

CREATE INDEX [IX_Person_HomeAddressID] ON [dbo].[Person] ([HomeAddressID])
GO

CREATE INDEX [IX_Person_WorkAddressID] ON [dbo].[Person] ([WorkAddressID])
GO

ALTER TABLE [dbo].[Person] ADD CONSTRAINT [FK_Person_HomeAddressID] FOREIGN KEY ([HomeAddressID]) REFERENCES [Address] ([HomeAddressID])
GO

ALTER TABLE [dbo].[Person] ADD CONSTRAINT [FK_Person_WorkAddressID] FOREIGN KEY ([WorkAddressID]) REFERENCES [Address] ([WorkAddressID])
GO
Jim73
  • 9
  • 1
  • possible duplicate of [Improve navigation property names when reverse engineering a database](http://stackoverflow.com/questions/12937193/improve-navigation-property-names-when-reverse-engineering-a-database) – Yuliam Chandra Aug 23 '14 at 13:58
  • Unfortunately for my situation, it looks like all solutions involve a form of code first / working from the model to the database. – Jim73 Aug 27 '14 at 13:04
  • that solution works for database first, I tried it many times, which part that doesn't work ? – Yuliam Chandra Aug 27 '14 at 13:06

1 Answers1

0

No, using database first, you will need to update the navigation names in the data model by-hand.

Of course you could modify the T4 file to generate a different navigation name (it uses the FK table name by default).

Carl Prothman
  • 1,461
  • 13
  • 23