MVC 4 Retreive db id from newly created entry
If I create a entry in a database such as this (cvmCasefile has all info
needed to create the casefile):
Casefile casefile = cvm.Casefile;
casefile.ClientId = cvm.Client.ClientId;
casefile.DateSubmitted = DateTime.Now;
db.Casefiles.Add(casefile);
db.SaveChanges();
Immediately after the save call I try to retrieve this entries ID number
from the database with:
int casefileId = db.Casefiles.Where(u => u.UserProfileId ==
casefile.UserProfileId)
.Where(c => c.ClientId == casefile.ClientId)
.Single(d => d.DateSubmitted ==
casefile.DateSubmitted).CasefileId;
This returns null when it is executed. I've stepped through the program
and all casefile values are populated and the database has the required
row inserted with a valid ID#. Is there an easier way to get the ID from
the database or where did is screw-up the call to the database?
No comments:
Post a Comment