3

鉴于我的代码的上下文片段:

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();

实体框架有没有办法在完成后获取为校准对象选择(播种)的主键db.SaveChanges();

换句话说,在提交后如何获取该对象的主键?

4

2 回答 2

5

在上下文中重新加载条目:

db.Entry(calibration).Reload();

然后就可以访问新创建的PK

于 2013-04-05T18:55:44.820 回答
2

此外,您可以从Calibration.PrimaryKeyPropertydb.SaveChanges(); 之后获取

方法,

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;
于 2013-04-05T19:00:57.807 回答