简体   繁体   English

带有多个键的EF Find()

[英]EF Find() with multiple Keys

I am trying to use Find() on a set of an EF class that has a foreign Key and 2 primary Keys, however I keep getting error. 我试图在具有外键和2个主键的EF类的集合上使用Find(),但是我一直遇到错误。

This is my class: 这是我的课:

public class dbDDV
    {
        [ForeignKey("dbf")]
        [Column(Order = 0)]
        public int id { get; set; }

        [Key]
        [Column(Order = 1)]
        public DateTime D0 { get; set; }

        [Key]
        [Column(Order = 2)]
        public DateTime D1 { get; set; }

        public double V{ get; set; }
    }

For a particular value of id, D0 and D1 I have been trying to use Find(): 对于id,D0和D1的特定值,我一直在尝试使用Find():

dbContext.mySetOfdbDDV.Find(myId,myD0,myD1)

The following is the error I receive: 以下是我收到的错误:

System.ArgumentException: The number of primary key values passed must match number of primary key values defined on the entity. System.ArgumentException:传递的主键值的数量必须与在实体上定义的主键值的数量匹配。

What would be the correct approach for Find() on this case? 在这种情况下,Find()的正确方法是什么?

Only D0 and D1 are part of your composite primary key (not id , as weird as that is, but I'm not here to judge your design). 只有D0D1是您的组合主键的一部分(不是id ,这很奇怪,但是我不是在这里判断您的设计)。 So you should only pass values for those 2 columns when invoking Find 因此,您仅应在调用Find时传递这2列的值

dbContext.mySetOfdbDDV.Find(myD0,myD1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM