I have two classes:
public partial class ShipmentRoutePointDb
{
public long Id { get; set; } // Primary key
public System.Guid Uuid { get; set; }
public virtual TransportInfoDb NextTransportInfo { get; set; }
}
public partial class TransportInfoDb
{
public TransportInfoDb()
{
this.CertifiedConsignments = new HashSet<CertifiedConsignmentDb>();
this.TransportInfoGroups = new HashSet<TransportInfoGroupDb>();
}
public long Id { get; set; }
public string VehicleNumber { get; set; }
public virtual ICollection<CertifiedConsignmentDb> CertifiedConsignments { get; set; }
public virtual ICollection<TransportInfoGroupDb> TransportInfoGroups { get; set; }
}
I want to update VehicleNumber of ShipmentRoutePointDb so I retrieve record for update:
var shipmentRoutePointDb = _shipmentRoutePointRepository.GetByUuid(updatedRoutePoint.Uuid);
then i need to check should i create new record in TransportInfoDb or there is already exists record with similar parameters:
var duplicate = transportInfoRepository.GetByParams(shipmentRoutePointDb.NextTransportInfo.VehicleNumber);
If i able to find such record i update shipmentRoutePointDb like this:
shipmentRoutePointDb.NextTransportInfo = duplicate;
shipmentRoutePointDb.NextTransportId = duplicate.Id;
_shipmentRoutePointRepository.Update2(shipmentRoutePointDb);
public void Update2(ShipmentRoutePointDb entity)
{
var entr = _context.Entry<ShipmentRoutePointDb>(entity);
entr.State = EntityState.Modified;
SaveChanges();
}
But SaveChanges() throws an exception:
System.InvalidOperationException: "A referential integrity constraint violation occurred: The property value(s) of 'TransportInfoDb.Id' on one end of a relationship do not match the property value(s) of 'ShipmentRoutePointDb.NextTransportId' on the other end."
I know that i haven't listed there some methods that i use, but they are working fine and their names is pretty self explanatory.