Fallingsappy
Fallingsappy

Reputation: 303

How to change related entity for another existing one with Entity Framework

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.

Upvotes: 4

Views: 188

Answers (2)

Fallingsappy
Fallingsappy

Reputation: 303

public void Update2(long transportId, long shipmentRouteId)
{
    var shipment = _context.ShipmentRoutePointDb.FirstOrDefault(x => x.Id == shipmentRouteId);            
    _context.ShipmentRoutePointDb.Attach(shipment);

    shipment.NextTransportId = transportId;

    _context.ChangeTracker.DetectChanges();

    SaveChanges();
}

this helped. The main reason was that detect changes is disabled on this project.

Upvotes: 2

Ali Kianoor
Ali Kianoor

Reputation: 1243

My Friend, I believe the only thing you need to do is an update like this by passing the id and parameter:

public void Update2(var id, type entity)
{
    var result = _context.tableName.SingleOrDefault(b => b.NextTransportId == id);
    if (result != null)
    {
        try
        {
            db.Entry(entity).State = EntityState.Modified;
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

Upvotes: 0

Related Questions