Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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():

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.

What would be the correct approach for Find() on this case?

like image 688
donquijote Avatar asked Dec 06 '22 17:12

donquijote


1 Answers

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). So you should only pass values for those 2 columns when invoking Find

dbContext.mySetOfdbDDV.Find(myD0,myD1)
like image 73
sstan Avatar answered Dec 20 '22 03:12

sstan