alecs9876
alecs9876

Reputation: 11

Error: The type of one of the primary key values did not match the type defined in the entity

I'm not sure why I am getting this error. I had it working before and now it's not. This is the inner exception error message:

EntitySqlException: The argument types 'Edm.Int32' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 78.

This is the line of code that it's singling out:

Attending attending = db.Attendings.Find(userId);

This is the code in the context of the controller:

public ActionResult Attendees(int? id)
    {
        var userId = User.Identity.GetUserId();

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Attending attending = db.Attendings.Find(userId);
        if (attending == null)
        {
            return HttpNotFound();
        }
        PopulateBeverageDropDownList(attending.BeverageId);

        return View(attending);
    }

I have looked all over stackoverflow and Google, but even though I find similar problems, I still cannot figure out how to solve my own.

Basically what I'm trying to do is to use the PK from one of my models and the userId from asp.net identity concurrently. This used to work fine, except I don't know what's changed for this error to pop up. This is the link to my repository to view the code in its entirety: https://github.com/alec9876/Firepit/tree/master/Firepit

Any help is appreciated!

Upvotes: 1

Views: 562

Answers (1)

user7217806
user7217806

Reputation: 2119

Find is used to get an entity via it's primary key. For the Attendees model the primary key is AttendeeID, which is, unlike the userId, an int. If you want to get the Attending by it's id, you can just pass the id variable to Find. If you want to get Attendings of a user, you could get the AttendeeID of the user, and use Where to get the Attendings, e.g.:

 db.Attendings.Where(x => x.AttendeeId == attendeeId)

Upvotes: 1

Related Questions