我正在开发一个 Windows 应用程序,我需要在网格中显示对象列表及其子对象。因此用户可以修改从实体框架中检索到的实体。
在更新数据库中的对象时,看起来需要从数据库中检索对象并分配新值。这可能会导致性能问题。有解决办法吗?
public class Vehicle
{
public Vehicle()
{
this.Fillups = new List<FillupEntry>();
this.Reminders = new List<Reminder>();
}
public int VehicleId { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
public string ModelName { get; set; }
public ICollection<FillupEntry> Fillups { get; set; }
public ICollection<Reminder> Reminders { get; set; }
}
public void Update(Vehicle updatedVehicle)
{
Vehicle vehicleToUpdate =
this.GetDbSet<Vehicle>()
.Where(v => v.VehicleId == updatedVehicle.VehicleId)
.First();
vehicleToUpdate.Name = updatedVehicle.Name;
vehicleToUpdate.Year = updatedVehicle.Year;
vehicleToUpdate.MakeName = updatedVehicle.MakeName;
vehicleToUpdate.ModelName = updatedVehicle.ModelName;
vehicleToUpdate.SortOrder = updatedVehicle.SortOrder;
vehicleToUpdate.PhotoId = updatedVehicle.PhotoId;
this.SetEntityState(vehicleToUpdate, vehicleToUpdate.VehicleId == 0
? EntityState.Added
: EntityState.Modified);
this.UnitOfWork.SaveChanges();
}