Reputation: 5150
I have two tables. Document table and Version table. Both are identicle except the version table has an ID field and a documentID field. The document table has a documentId field.
I can correctly find the document but I cannot find the version table information because the id I am padding in it is trying to find this on the id field instead of the documentId field.
public ActionResult ApproveDocument(int id = 0)
{
IPACS_Document ipacs_document = db.IPACS_Document.Find(id);
IPACS_Version ipacs_version = db.IPACS_Version.Find(id);
ipacs_version.dateApproved = System.DateTime.Now;
ipacs_version.approvedBy = User.Identity.Name.Split("\\".ToCharArray())[1];
ipacs_document.dateApproved = System.DateTime.Now;
ipacs_document.approvedBy = User.Identity.Name.Split("\\".ToCharArray())[1];
ipacs_document.revision = ipacs_version.revision;
db.SaveChanges();
return RedirectToAction("Approve");
}
So the ipacs_document
is found correctly because the id passed in 11 works. However ipacs_version
doesn't find anything because it is trying to find id 11
instead of documentId 11
.
Upvotes: 3
Views: 994
Reputation: 14302
If you're wondering on how to use Find
(DbSet<>) to engage composite keys
...
The Find method takes an array of objects as an argument. When working with composite primary keys, pass the key values separated by commas and in the same order that they are defined in the model.
http://msdn.microsoft.com/en-us/library/gg696418(v=vs.103).aspx
db.IPACS_Version.Find(id, documentid); // mind the order
And for anything more complex keep in mind that you could always use Linq queries, Where
e.g.
db.IPACS_Version.Where(x => x.Id == id && x.DocumentId == docid && x.Flag == true);
Note: You could use the
query
,Where
(regardless of how your entities are made) -
but if yourkeys
are not set up properly (based on the comments) - I'd discourage you to go that way. Instead of a fast fix, make sure your tables, pk-s are set up as they should - as that's essential. Then you can see which query is best for you (or just use Find if that's all you need).
Upvotes: 1