user2739418
user2739418

Reputation: 1631

Foreign key on the same properties already exists

I have Table1 and Table2.

Table1 has a foreignKey pointing to Table2 primary Key. It could be a bad design, but I cannot change it as it is old.

Now I want to use EF with code first and tried to generate a model and DbContext classes for it through the following command:

dotnet ef dbcontext scaffold "Server=Server1;Database=DBName;Integrated Security=True;MultiSubnetFailover=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

And getting the following error:

The foreign key {'ForeignKeyId'} cannot be added to the entity type 'TABLENAME' because a foreign key on the same properties already exists on entity type 'TABLENAME' and also targets the key {'FOREIGNKEY'} on 'FOREIGNKEY_PrimaryTable'.

Is there a way I can overcome this error?

Upvotes: 3

Views: 3437

Answers (1)

Kadaj
Kadaj

Reputation: 705

I don't know if you managed to make this work, or if you need it at all after all this time, but since I got here from Google (top 3 results) I will write up how I solved this issue. Maybe it helps someone in the future.

For me it was because I had 2 foreign keys, but 3 constraints were generated for them.

After looking at scripts for given table:

CREATE TABLE [dbo].[VacationBooking](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [VacationType] [bigint] NOT NULL,
    [StartDateTime] [datetime] NOT NULL,
    [EndDateTime] [datetime] NOT NULL,
    [User] [bigint] NOT NULL,
    [Employee] [bigint] NOT NULL,
    [Status] [int] NOT NULL CONSTRAINT [DF_VacationBooking_Status]  DEFAULT ((0)),
    [IsSameDayVacation] [bit] NOT NULL CONSTRAINT [DF_VacationBooking_IsSameDayVacation]  DEFAULT ((0)),
    [NumberOfHoursAbsent] [decimal](5, 2) NOT NULL CONSTRAINT [DF_VacationBooking_NumberOfHoursAbsent]  DEFAULT ((0.00))    
 CONSTRAINT [PK_VacationBooking] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO


-- Constraint 1
ALTER TABLE [dbo].[VacationBooking]  WITH CHECK ADD  CONSTRAINT [FK_VacationBooking_User] FOREIGN KEY([User])
REFERENCES [dbo].[User] ([Id])
GO

ALTER TABLE [dbo].[VacationBooking] CHECK CONSTRAINT [FK_VacationBooking_User]
GO

-- Constraint 2
ALTER TABLE [dbo].[VacationBooking]  WITH CHECK ADD  CONSTRAINT [FK_VacationBooking_User1] FOREIGN KEY([Employee])
REFERENCES [dbo].[User] ([Id])
GO

ALTER TABLE [dbo].[VacationBooking] CHECK CONSTRAINT [FK_VacationBooking_User1]
GO


-- Constraint 3
ALTER TABLE [dbo].[VacationBooking]  WITH CHECK ADD  CONSTRAINT [FK_VacationBooking_User2] FOREIGN KEY([Employee])
REFERENCES [dbo].[User] ([Id])
GO

ALTER TABLE [dbo].[VacationBooking] CHECK CONSTRAINT [FK_VacationBooking_User2]
GO

I need to have 2 FK's to User table (one for author, and one for employee whom it concerns), but there should only be then 2 constraints.

After I deleted

Constraint 3

from my table, I could successfully scaffold database.

Upvotes: 3

Related Questions