首先使用相同的表EF代码创建具有多个外键的复合键 [英] Creating composite key with multiple foreign keys from same table EF code first

查看:136
本文介绍了首先使用相同的表EF代码创建具有多个外键的复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,



我在EF6上首先遇到Code问题。



I尝试创建迁移时出现此错误关系约束中的从属角色和主要角色中的属性数必须相同。



以下是代码:



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



  }





我正在尝试创建一个复合键,用两个外键(OpenTime,CloseTime)引用同一个表(HistoricalTrade)。

我失去了理智大声笑。

这是我最近的尝试,使历史贸易被引用两次,一次是s,一次没有。我现在正在做任何事情。

任何人都知道如何解决这个问题?而对于奖励积分,请用简单的术语解释我得到的错误,这样我就可以了解被引用的内容(IE:什么是依赖角色,主要角色和关系约束(虽然我猜这只是FK)?)。



我觉得我的已经尝试过的资源可能有一个答案,我只是因为我没有理解如何实现而没有实现正确它:/



我尝试了什么:



阅读本< a href =https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx> DataAnnotations - EF 7中的ForeignKey属性& EF Core [ ^ ]



阅读本文 DataAnnotations - 代码优先的InverseProperty属性 [ ^ ]



这个也是:asp.net mvc - 定义多个外键实体框架代码中的相同表格 - 堆栈溢出 [ ^ ]



这个: c# - 实体框架代码优先 - 来自同一个表的两个外键 - Stack Overflow [ ^ ]



此外:两个表之间的多个关系/外键(在类中)| ASP.NET论坛 [ ^ ]



以及其他一些SO帖子,我没有更多的链接(或者记住我为搜索结果输入的内容)。



我尝试使用Inverse Property但是在我的具体情况下如何正确实现这一点没有意义?

我已将其更改为虚拟的HistoricalTrade并将两个外键=HistoricalTrade同时命名为HistoricalTrade_OpenTime。没用。



我在这里不知所措。

任何帮助表示感谢。



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.

推荐答案

根据您的课程,您有一对一的关系。



实体框架 - 使用复合键映射引用| 2月开发 [ ^ ]



c# - 复合键上的实体框架代码第一对一关系 - Stack Overflow [ ^ ]
You have a one-to-one relationship according to you classes.

Entity Framework – mapping references with composite keys | Feb dev[^]

c# - Entity Framework Code First One to One relationship on composite key - Stack Overflow[^]


这篇关于首先使用相同的表EF代码创建具有多个外键的复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆