简体   繁体   English

如何使用实体框架更改另一个现有实体的相关实体

[英]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:我想更新 ShipmentRoutePointDb 的 VehicleNumber,因此我检索记录以进行更新:

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:然后我需要检查是否应该在 TransportInfoDb 中创建新记录,或者已经存在具有类似参数的记录:

var duplicate = transportInfoRepository.GetByParams(shipmentRoutePointDb.NextTransportInfo.VehicleNumber);

If i able to find such record i update shipmentRoutePointDb like this:如果我能找到这样的记录,我会像这样更新 shipRoutePointDb:

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:但是 SaveChanges() 抛出异常:

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." System.InvalidOperationException:“发生引用完整性约束违规:关系一端的“TransportInfoDb.Id”的属性值与另一端的“ShipmentRoutePointDb.NextTransportId”的属性值不匹配。 "

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.我知道我没有列出我使用的一些方法,但它们运行良好,而且它们的名称很容易解释。

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.主要原因是该项目禁用了检测更改。

My Friend, I believe the only thing you need to do is an update like this by passing the id and parameter:我的朋友,我相信您唯一需要做的就是通过传递 id 和参数来进行这样的更新:

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM