如何更新具有一对一关系的实体?
How to update Entity that have one-to-one relation?
我有如下实体:
public class Device
{
public long Guid { get; set; }
public List<Computer> Computers { get; set; }
public string ShopName { get; set; }
}
public class Computer
{
public long Guid { get; set; }
public long SerialNo { get; set; }
public Device Device { get; set; }
public Condition Condition { get; set; }
}
public class Condition
{
public long Guid { get; set; }
public bool hasDamage { get; set; }
public bool IsSecondHand { get; set; }
public Computer Computer { get; set; }
}
我的服务请求是:
public class Request
{
public long? DeviceGuid {get; set;}
public long? SerialNo {get; set;}
public bool? IsSecondHand {get; set;}
}
我要根据要求更新所有电脑
foreach (var requestItem in RequestList)
{
var ComputerPredicate = PredicateBuilder.True<Computer>()
.And(x => x.SerialNo== requestItem.SerialNo)
.And(x => x.Device.Guid== requestItem.DeviceGuid);
var computers = from computers in ISession.Query<Computer>()
.Where(ComputerPredicate)
select computers;
computers.Update(u => new Computer()
{
Condition = new Condition()
{
IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false),
}
});
如果Request.DeviceGuid
不为空,我更新属于该设备的所有计算机;如果 Request.SerialNo
不为空,我只更新计算机。其中一个在列表的每一项中总是为空。
但是我收到一个错误
NHibernate.Exceptions.GenericADOException: 'could not execute update query[SQL: update COMPUTER set Condition.IsSecondHand=? where and serialno=?]'
PostgresException: 42703: column "condition" of relation "computer" does not exist
SQL确实没有关系。
还有一个选项我可以成功更新,但我不确定这是一个有效的方法:
var computerList = computers.ToList();
foreach (var computer in computerList)
{
computer.Condition.IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false);
ISession.Save(computer)
}
那么我怎样才能最有效地处理这种情况呢?
NHibernate 中的 LINQ 更新不支持关系实体的更新。您可以尝试使用子查询来查找您需要更新的所有实体。类似于:
var entitiesToUpdateSubQuery = session.Query<Computer>()
.Where(ComputerPredicate)
.Select(x => x.Condition); //<- Select entity you need to update
session.Query<Condition>() //<- Write update query directly on entity you need to update
.Where(c => entitiesToUpdateSubQuery.Contains(c))
.Update(c =>
new Condition()
{
IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false),
}
);
我有如下实体:
public class Device
{
public long Guid { get; set; }
public List<Computer> Computers { get; set; }
public string ShopName { get; set; }
}
public class Computer
{
public long Guid { get; set; }
public long SerialNo { get; set; }
public Device Device { get; set; }
public Condition Condition { get; set; }
}
public class Condition
{
public long Guid { get; set; }
public bool hasDamage { get; set; }
public bool IsSecondHand { get; set; }
public Computer Computer { get; set; }
}
我的服务请求是:
public class Request
{
public long? DeviceGuid {get; set;}
public long? SerialNo {get; set;}
public bool? IsSecondHand {get; set;}
}
我要根据要求更新所有电脑
foreach (var requestItem in RequestList)
{
var ComputerPredicate = PredicateBuilder.True<Computer>()
.And(x => x.SerialNo== requestItem.SerialNo)
.And(x => x.Device.Guid== requestItem.DeviceGuid);
var computers = from computers in ISession.Query<Computer>()
.Where(ComputerPredicate)
select computers;
computers.Update(u => new Computer()
{
Condition = new Condition()
{
IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false),
}
});
如果Request.DeviceGuid
不为空,我更新属于该设备的所有计算机;如果 Request.SerialNo
不为空,我只更新计算机。其中一个在列表的每一项中总是为空。
但是我收到一个错误
NHibernate.Exceptions.GenericADOException: 'could not execute update query[SQL: update COMPUTER set Condition.IsSecondHand=? where and serialno=?]'
PostgresException: 42703: column "condition" of relation "computer" does not exist
SQL确实没有关系。
还有一个选项我可以成功更新,但我不确定这是一个有效的方法:
var computerList = computers.ToList();
foreach (var computer in computerList)
{
computer.Condition.IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false);
ISession.Save(computer)
}
那么我怎样才能最有效地处理这种情况呢?
NHibernate 中的 LINQ 更新不支持关系实体的更新。您可以尝试使用子查询来查找您需要更新的所有实体。类似于:
var entitiesToUpdateSubQuery = session.Query<Computer>()
.Where(ComputerPredicate)
.Select(x => x.Condition); //<- Select entity you need to update
session.Query<Condition>() //<- Write update query directly on entity you need to update
.Where(c => entitiesToUpdateSubQuery.Contains(c))
.Update(c =>
new Condition()
{
IsSecondHand = requestItem.IsSecondHand.GetValueOrDefault(false),
}
);