Click here to Skip to main content
14,239,003 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
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.
Posted
Updated 2-May-19 2:31am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
OvrLrdFregus 27-Apr-19 22:28pm
   
Thanks for the Help Gerry!
This is my new error:
The property 'OpenTime' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<t> where T is a valid entity type.

This is what I did, trying to implement modelBuilder, as I have never messed with Fluent API since I preferred Data Annotations:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<tradejournal>()
.HasRequired(t => t.HistoricalTrade)
.WithMany()
.HasForeignKey(p => new { p.OpenTime, p.CloseTime });
}

This is under ApplicationDbContext, as the first link said to place it in the class that inherits from DbContext, and as far as I can tell, thats the only one getting close to it (under IdentityModel, when starting a fresh web app with .net mvc5).

Did I do something wrong with this? Or is this new error because TradeJournal has an issue with 'OpenTime'?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100