Given this out of context snippet of my code:
var calibration = new Calibration
{
CalibrationType = SelectedTest.TestTypeId
,Expiration = expirationDate
,LastSaved = DateTime.Now
,StatusTypeId = 1
,TechnicianId = SelectedTechnician.Id
,Phases = BuildCalibrationPhases()
};
db.Calibrations.Add(calibration);
db.SaveChanges();
Is there a way with Entity Framework to get the primary key that was chosen(seeded) for the calibration object after db.SaveChanges(); completes?
In other words how can I get the primary key for this object after it is committed?
Reload the entry in the context:
db.Entry(calibration).Reload();
Then you can access the newly created PK
Also, You can get from Calibration.PrimaryKeyProperty after db.SaveChanges();
Means,
var calibration = new Calibration
{
CalibrationType = SelectedTest.TestTypeId
,Expiration = expirationDate
,LastSaved = DateTime.Now
,StatusTypeId = 1
,TechnicianId = SelectedTechnician.Id
,Phases = BuildCalibrationPhases()
};
db.Calibrations.Add(calibration);
db.SaveChanges();
int value = Calibration.PrimaryKeyProperty;
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