I have the following model and am trying to find a specific object in a DbSet
:
public class UserSkill
{
[Key, Column(Order = 1)]
public int UserId { get; set; }
[Key, Column(Order = 2)]
[ForeignKey("Skill")]
public int SkillId { get; set; }
public virtual Skill Skill { get; set; }
}
I've tried the following two ways of finding a certain UserSkill
object (I'm passing the DbSet
of UserSkills
via the ViewBag
):
ViewBag.UserSkills.Find(new { WebSecurity.CurrentUserId, item.SkillId })
ViewBag.UserSkills.Find(new UserSkill(WebSecurity.CurrentUserId, item.SkillId))
But in both cases, I get an error:
The number of primary key values passed must match number of primary key values defined on the entity.
What am I missing? It seems to me that the primary key consists of two columns, and I'm providing the find function with the two values that comprise the primary key.
The signature of Find
method is TEntity Find(params Object[] keyValues)
and you can enlist the keys of composite primary key in proper order.
ViewBag.UserSkills.Find(WebSecurity.CurrentUserId, item.SkillId )
To find an entity by composite key you should use this overload
ViewBag.UserSkills.Find(WebSecurity.CurrentUserId, item.SkillId);
Msdn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With