2 same foreign keys in one table. Fluent Api generate 3 foreign key instead of 2Inheritance in database creates unnecessary foreign key column in superclass tableEF6: Foreign keys are not recognized on model generation for Sql Server DBSetting unique Constraint with fluent API?Entity Framework seeding causes insert statement conflict with foreign keyMany to one migrations fails on foreign key longComposite Key EF Core getting error when using Fluent ApiMultiple rows with same foreign key causing foreign key constraint errorEF CORE - Fluent API - cascading delete restrict to on tableEntity Framework Core assign foreign key constraint in OnModelCreating?System.Data.SqlClient.SqlException: 'The INSERT statement conflicted with the FOREIGN KEY constraint
Quick Slitherlink Puzzles: KPK and 123
Get rows that exist exactly once per day for a given period
How secure are public hashed passwords (with a salt)?
Why do we need explainable AI?
Doesn't the concept of marginal utility speak to a cardinal utility function?
Is it good practice to speed up and slow down where not written in a song?
How to find better food in airports
Tasha's Hideous Laughter used on a deaf person?
Using large parts of a research paper
What is causing gaps in logs?
Can a system of three stars exist?
Squares inside a square
How would a disabled person earn their living in a medieval-type town?
garage light with two hots and one neutral
Polarity of gas discharge tubes?
Is there anything in the universe that cannot be compressed?
How are the cards determined in an incomplete deck of many things?
What is the practical impact of using System.Random which is not cryptographically random?
Using font to highlight a god's speech in dialogue
Can UV radiation be safe for the skin?
Cannot add javascript to footer
Ways you can end up paying interest on a credit card if you pay the full amount back in due time
Divide Numbers by 0
In Toy Story, are toys the only inanimate objects that become alive? And if so, why?
2 same foreign keys in one table. Fluent Api generate 3 foreign key instead of 2
Inheritance in database creates unnecessary foreign key column in superclass tableEF6: Foreign keys are not recognized on model generation for Sql Server DBSetting unique Constraint with fluent API?Entity Framework seeding causes insert statement conflict with foreign keyMany to one migrations fails on foreign key longComposite Key EF Core getting error when using Fluent ApiMultiple rows with same foreign key causing foreign key constraint errorEF CORE - Fluent API - cascading delete restrict to on tableEntity Framework Core assign foreign key constraint in OnModelCreating?System.Data.SqlClient.SqlException: 'The INSERT statement conflicted with the FOREIGN KEY constraint
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
My problem is in EF Core 2.2 with Fluent API. I need to generate 3 foreign keys in one table instead of 2, it generates EmployeeId
that is not required for my table.
Here is my code:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public int ToEmployeeId get; set;
public Employee Employee get; set;
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackings get; set;
I also tried this:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.FromEmployee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.ToEmployee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public Employee FromEmployee get; set;
public int ToEmployeeId get; set;
public Employee ToEmployee get; set;
Here is the generated Table by EF Core
CREATE TABLE [Communication].[RequestTracking]
(
[Id] INT IDENTITY(1, 1) NOT NULL,
[RequestId] INT NOT NULL,
[FromEmployeeId] INT NOT NULL,
[ToEmployeeId] INT NOT NULL,
[EmployeeId] INT NULL, // Here is my problem it generates this column
CONSTRAINT [PK_RequestTracking]
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_RequestTracking_Employee_EmployeeId]
FOREIGN KEY ([EmployeeId]) REFERENCES [HR].[Employee] ([Id]),
CONSTRAINT [FK_RequestTracking_Request_RequestId]
FOREIGN KEY ([RequestId]) REFERENCES [Communication].[Request] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_RequestTracking_Employee_ToEmployeeId]
FOREIGN KEY ([ToEmployeeId]) REFERENCES [HR].[Employee] ([Id])
);
c# entity-framework-core ef-fluent-api
add a comment |
My problem is in EF Core 2.2 with Fluent API. I need to generate 3 foreign keys in one table instead of 2, it generates EmployeeId
that is not required for my table.
Here is my code:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public int ToEmployeeId get; set;
public Employee Employee get; set;
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackings get; set;
I also tried this:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.FromEmployee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.ToEmployee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public Employee FromEmployee get; set;
public int ToEmployeeId get; set;
public Employee ToEmployee get; set;
Here is the generated Table by EF Core
CREATE TABLE [Communication].[RequestTracking]
(
[Id] INT IDENTITY(1, 1) NOT NULL,
[RequestId] INT NOT NULL,
[FromEmployeeId] INT NOT NULL,
[ToEmployeeId] INT NOT NULL,
[EmployeeId] INT NULL, // Here is my problem it generates this column
CONSTRAINT [PK_RequestTracking]
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_RequestTracking_Employee_EmployeeId]
FOREIGN KEY ([EmployeeId]) REFERENCES [HR].[Employee] ([Id]),
CONSTRAINT [FK_RequestTracking_Request_RequestId]
FOREIGN KEY ([RequestId]) REFERENCES [Communication].[Request] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_RequestTracking_Employee_ToEmployeeId]
FOREIGN KEY ([ToEmployeeId]) REFERENCES [HR].[Employee] ([Id])
);
c# entity-framework-core ef-fluent-api
add a comment |
My problem is in EF Core 2.2 with Fluent API. I need to generate 3 foreign keys in one table instead of 2, it generates EmployeeId
that is not required for my table.
Here is my code:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public int ToEmployeeId get; set;
public Employee Employee get; set;
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackings get; set;
I also tried this:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.FromEmployee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.ToEmployee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public Employee FromEmployee get; set;
public int ToEmployeeId get; set;
public Employee ToEmployee get; set;
Here is the generated Table by EF Core
CREATE TABLE [Communication].[RequestTracking]
(
[Id] INT IDENTITY(1, 1) NOT NULL,
[RequestId] INT NOT NULL,
[FromEmployeeId] INT NOT NULL,
[ToEmployeeId] INT NOT NULL,
[EmployeeId] INT NULL, // Here is my problem it generates this column
CONSTRAINT [PK_RequestTracking]
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_RequestTracking_Employee_EmployeeId]
FOREIGN KEY ([EmployeeId]) REFERENCES [HR].[Employee] ([Id]),
CONSTRAINT [FK_RequestTracking_Request_RequestId]
FOREIGN KEY ([RequestId]) REFERENCES [Communication].[Request] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_RequestTracking_Employee_ToEmployeeId]
FOREIGN KEY ([ToEmployeeId]) REFERENCES [HR].[Employee] ([Id])
);
c# entity-framework-core ef-fluent-api
My problem is in EF Core 2.2 with Fluent API. I need to generate 3 foreign keys in one table instead of 2, it generates EmployeeId
that is not required for my table.
Here is my code:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public int ToEmployeeId get; set;
public Employee Employee get; set;
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackings get; set;
I also tried this:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.FromEmployee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.ToEmployee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
public int Id get; set;
public int RequestId get; set;
public Request Request get; set;
public int FromEmployeeId get; set;
public Employee FromEmployee get; set;
public int ToEmployeeId get; set;
public Employee ToEmployee get; set;
Here is the generated Table by EF Core
CREATE TABLE [Communication].[RequestTracking]
(
[Id] INT IDENTITY(1, 1) NOT NULL,
[RequestId] INT NOT NULL,
[FromEmployeeId] INT NOT NULL,
[ToEmployeeId] INT NOT NULL,
[EmployeeId] INT NULL, // Here is my problem it generates this column
CONSTRAINT [PK_RequestTracking]
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_RequestTracking_Employee_EmployeeId]
FOREIGN KEY ([EmployeeId]) REFERENCES [HR].[Employee] ([Id]),
CONSTRAINT [FK_RequestTracking_Request_RequestId]
FOREIGN KEY ([RequestId]) REFERENCES [Communication].[Request] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_RequestTracking_Employee_ToEmployeeId]
FOREIGN KEY ([ToEmployeeId]) REFERENCES [HR].[Employee] ([Id])
);
c# entity-framework-core ef-fluent-api
c# entity-framework-core ef-fluent-api
edited Mar 28 at 5:01
marc_s
603k136 gold badges1152 silver badges1289 bronze badges
603k136 gold badges1152 silver badges1289 bronze badges
asked Mar 28 at 0:53
Marco TorrecampoMarco Torrecampo
84 bronze badges
84 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I Figure Out what is the problem, I add 2 ICollection<RequestTracking>
in Employee
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackingsFrom get; set;
public ICollection<RequestTracking> RequestTrackingsTo get; set;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388619%2f2-same-foreign-keys-in-one-table-fluent-api-generate-3-foreign-key-instead-of-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I Figure Out what is the problem, I add 2 ICollection<RequestTracking>
in Employee
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackingsFrom get; set;
public ICollection<RequestTracking> RequestTrackingsTo get; set;
add a comment |
I Figure Out what is the problem, I add 2 ICollection<RequestTracking>
in Employee
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackingsFrom get; set;
public ICollection<RequestTracking> RequestTrackingsTo get; set;
add a comment |
I Figure Out what is the problem, I add 2 ICollection<RequestTracking>
in Employee
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackingsFrom get; set;
public ICollection<RequestTracking> RequestTrackingsTo get; set;
I Figure Out what is the problem, I add 2 ICollection<RequestTracking>
in Employee
public class Employee
public int Id get; set;
public string FirstName get; set;
public string MiddleName get; set;
public string LastName get; set;
public DateTime Birthdate get; set;
public Gender Gender get; set;
public AppUser User get; set;
public ICollection<EmployeeStatus> EmployeesStatus get; set;
public ICollection<Resignation> Resignations get; set;
public ICollection<RequestTracking> RequestTrackingsFrom get; set;
public ICollection<RequestTracking> RequestTrackingsTo get; set;
answered Mar 28 at 1:03
Marco TorrecampoMarco Torrecampo
84 bronze badges
84 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388619%2f2-same-foreign-keys-in-one-table-fluent-api-generate-3-foreign-key-instead-of-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown