E
E
Exxplozer2014-12-19 01:30:58
.NET
Exxplozer, 2014-12-19 01:30:58

Why does Entity Framework create new keys?

Hello.
Created a database in visual and (db.mdf), created connection tables and even filled the database with test records.
Now you need to connect the Entity Framework. I create a model (ADO.NET Entity Data Model), I generate it according to my database. Everything is going well.
After starting the application, it creates a database similar to me in SQL Server, saves it to the user's documents on the computer. But here's what's strange in every table where there were foreign keys, another one was created and the links are tied to them.
For example
it was:

[Id]            INT           IDENTITY (1, 1) NOT NULL,
    [FirstName]     NVARCHAR (50) NOT NULL,
    [SecondName]    NVARCHAR (50) NOT NULL,
    [PositionId]    INT          NOT NULL,
    [GroupId]       INT          NOT NULL,
    CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Employees_Group] FOREIGN KEY ([GroupId]) REFERENCES [dbo].[Groups] ([Id]),
    CONSTRAINT [FK_Employees_Position] FOREIGN KEY ([PositionId]) REFERENCES [dbo].[Positions] ([Id])

It became:
[Id]          INT           IDENTITY (1, 1) NOT NULL,
    [FirstName]     NVARCHAR (MAX) NULL,
    [SecondName]    NVARCHAR (MAX) NULL,
    [PositionId]    INT         NOT  NULL,
    [GroupId]       INT          NOT NULL,
    [Positions_Id]  INT            NULL,
    [Groups_Id]     INT            NULL,
CONSTRAINT [PK_dbo.Employees] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_dbo.Employees_dbo.Groups_Groups_Id] FOREIGN KEY ([Groups_Id]) REFERENCES [dbo].[Groups] ([Id]),
    CONSTRAINT [FK_dbo.Employees_dbo.Positions_Positions_Id] FOREIGN KEY ([Positions_Id) REFERENCES [dbo].[Positions] ([Id])

Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayorovp, 2014-12-19
@mayorovp

Did you accidentally rename the corresponding fields in the model?
In any case, you can always force the required field names either through attributes, or through mapping, or through conventions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question