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;








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])
);









share|improve this question
































    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])
    );









    share|improve this question




























      0












      0








      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])
      );









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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

























          1 Answer
          1






          active

          oldest

          votes


















          0















          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;






          share|improve this answer
























            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
            );



            );













            draft saved

            draft discarded


















            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









            0















            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;






            share|improve this answer





























              0















              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;






              share|improve this answer



























                0














                0










                0









                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;






                share|improve this answer













                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;







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 1:03









                Marco TorrecampoMarco Torrecampo

                84 bronze badges




                84 bronze badges





















                    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.



















                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현