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.