您好我正在尝试更新具有多个主键的对象
public class Sublots
{
public Int64 ID { get; set; }
public string SubLot { get; set; }
[ForeignKey("Lathe"), Column(Order = 1)]
public int LatheNo { get; set; }
[ForeignKey("Lathe"), Column(Order = 0)]
public int? LineID { get; set; }
public virtual Lathe Lathe { get; set; }
}
public class Lathe
{
[Key, Column(Order = 0)]
public int LineID { get; set; }
[Key, Column(Order = 1)]
public int LatheNo { get; set; }
public string Name { get; set; }
}
如果我尝试将LatheNo更改为另一个有效的LatheNo:
objSublot.LatheNo = 99;
_dbContext.SaveChanges();
我收到以下错误:
System.InvalidOperationException:'属性'LatheNo'是对象密钥信息的一部分,无法修改。 “
我在保存之前尝试将Lathe指定为null,但是我得到了相同的错误
查看型号:
private Sublots _objSublot;
public Sublots objSublot
{
get { return _objSublot; }
set { _objSublot = value; RaisePropertyChanged(nameof(objSublot)); }
}
public PuckViewModel(IPuckDataService dataService, IDialogCoordinator dialogCoordinator)
{
_dataService = dataService;
objSublot = _dataService.GetSublotFromPuckRFID("E004015045D5B5BE"); // To simplify code
}
的DataService:
public Sublots GetSublotFromPuckRFID(string strRfid)
{
Puck puck = _dbContext.Pucks.Where(p => p.RFID == strRfid).FirstOrDefault();
if(puck != null && puck.ID > 0)
{
return _dbContext.Sublots.Where(s => s.PuckID == puck.ID).OrderByDescending(p => p.ID).FirstOrDefault();
}
else
{
return null;
}
}
public bool SaveSublot(Sublots objSublot)
{
bool answer = false;
if (objSublot != null)
{
objSublot.LatheNo = 13; // Change here to simplify code
_dbContext.SaveChanges(); //Error here
answer = true;
}
return answer;
}