R13
R13

Reputation: 131

The model already has an element that has the same name - ASP.NET

I'm working with ASP.Net web application and whenever I tried to add a FOREIGN KEY this error appears in the Data tools operations :

SQL71508 :: The model already has an element that has the same name dbo.FK_Sellers_Users. SQL71508 :: The model already has an element that has the same name dbo.FK_Sellers_Users.

I don't understand what's the problem with FK! I have 2 tables with this error

table Sellers :

CREATE TABLE [dbo].[Sellers] (
    [Seller_ID]     INT           IDENTITY (1, 1) NOT NULL,
    [Name]          NVARCHAR (50) NOT NULL,
    [SUsername]      NVARCHAR (50) NOT NULL,
    [Password]      NVARCHAR (50) NOT NULL,
    [SEmail]         NVARCHAR (50) NOT NULL,
    [Phone]         NVARCHAR (50) NOT NULL,
    [City]          NVARCHAR (50) NOT NULL,
    [LastLoginDate] DATETIME      NULL,
    [CreatedDate]   DATETIME      NULL,
    PRIMARY KEY CLUSTERED ([Seller_ID] ASC), 
    CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SEmail]) REFERENCES [Users]([Email]), 
    CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SUsername]) REFERENCES [Users]([Username])
);

and table Users :

CREATE TABLE [dbo].[Users] (
    [Id]       INT           NOT NULL IDENTITY,
    [Username] NVARCHAR (50) NOT NULL,
    [Password] NVARCHAR (50) NOT NULL,
    [Email]    NVARCHAR (50) NOT NULL,
    [UserType] INT    NULL,
    PRIMARY KEY CLUSTERED ([Id]), 
    CONSTRAINT [AK_Users_Username] UNIQUE ([Username]), 
    CONSTRAINT [AK_Users_Email] UNIQUE ([Email]),
);

Upvotes: 5

Views: 21420

Answers (4)

ARLibertarian
ARLibertarian

Reputation: 167

If your project uses SSDT (SQL Server Data Tools), you probably have a foreign key defined in a folder for Keys. Look in "DB Name\Schema Objects\Schemas\dbo\Tables\Keys."

Then Look in the table definition. "DB Name\Schema Objects\Schemas\dbo\Tables". The key could be defined in BOTH places.

Upvotes: 1

Jimmy Oku
Jimmy Oku

Reputation: 1

Actually, this has to do with data that was already entered. Some of the PK and FK data that were entered before might have the same values. You need to delete those first and make the changes you need.

Upvotes: 0

Sylver
Sylver

Reputation: 11

You have two foreign keys with the same name FK_Sellers_Users. You should better use FK_Sellers_Users_Email and FK_Sellers_Users_Username.

Upvotes: 0

Jim
Jim

Reputation: 6881

Right there in your CREATE TABLE statement for dbo.Sellers, you have two FK constraints named FK_Sellers_Users.

Make those names unique, perhaps by adding the column name on the end.

Upvotes: 5

Related Questions