Click here to Skip to main content
15,885,032 members

Creating composite key with multiple foreign keys from same table EF code first

OvrLrdFregus asked:

Open original thread
Hey all,

I am having an issue with Code first on EF6.

I get this error when trying to create a migration "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical."

Here is the code:

public class TradeJournal
   {
       [Key, Column(Order = 0)]
       public int UserID { get; set; }
       [ForeignKey("UserID")]
       public UserAccount UserAccount { get; set; }

       [Key, Column(Order = 1)]
       public int AccountNumber { get; set; }
       [ForeignKey("AccountNumber")]
       public FxAccount FxAccount { get; set; }

       [Key, Column(Order = 2),ForeignKey("OpenTime")]
       public string OpenTime { get; set; }
       public HistoricalTrade HistoricalTrades { get; set; }

       [Key, Column(Order = 3), ForeignKey("CloseTime")]
       public string CloseTime { get; set; }
       public HistoricalTrade HistoricalTrade { get; set; }
       public string Entry { get; set; }

       public byte[] Image { get; set; }
   }


Which is referencing this class here:

public class HistoricalTrade
  {
      [Key, Column(Order = 0)]
      public int UserID { get; set; }
      [ForeignKey("UserID")]
      public UserAccount UserAccount { get; set; }

      [Key, Column(Order = 1)]
      public int AccountNumber { get; set; }
      [ForeignKey("AccountNumber")]
      public FxAccount FxAccount { get; set; }

      [Key, Column(Order = 2)]
      [JsonProperty("openTime")]
      public string OpenTime { get; set; }

      [Key, Column(Order = 3)]
      [JsonProperty("closeTime")]
      public string CloseTime { get; set; }



  }


I am trying to create a composite key made with two foreign keys (OpenTime,CloseTime) referencing the same table (HistoricalTrade).
I am losing my mind lol.
This was my latest attempt at it, making historicalTrade referenced twice, one with an s and one without.I am doing anything at this point.
Anyone have an idea how to fix this issue? And for bonus points, explain the error I am getting in simpler terms so I may learn what is being referenced(IE: What is a Dependent Role, Principal Role, and relationship constraint (though I am guessing this is just FKs)?).

I do feel like one of my "have tried" resources may have had an answer that I just didn't implement correct simply because I failed to understand how to implement it :/

What I have tried:

Reading this DataAnnotations - ForeignKey Attribute in EF 7 & EF Core[^]

Reading this DataAnnotations - InverseProperty Attribute in Code-First[^]

This one as well : asp.net mvc - Defining multiple Foreign Key for the Same table in Entity Framework Code First - Stack Overflow[^]

And this :c# - Entity Framework Code First - two Foreign Keys from same table - Stack Overflow[^]

Also this : Multiple Relationships/Foreign Keys Between Two Tables (in Classes) | The ASP.NET Forums[^]

And a few other SO post that I don't have the link for any more (or remember what I typed for the search result).

I attempted using Inverse Property but it didn't make sense in my specific case on how to implement this correctly?
I've changed it to a virtual HistoricalTrade and made both foreign keys = "HistoricalTrade" while naming it HistoricalTrade_OpenTime, for example. Didn't work.

I am at a loss here.
Any help appreciated.
Tags: C# (C# 6.0), SQL, Entity Framework ( Entity Framework 6.0), ASP.NET4.0, CodeFirst

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900