gpt4 book ai didi

c# - 如何更新 Entity Framework 中的大对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:54 25 4
gpt4 key购买 nike

在我的项目中,我的实体模型如下所示:

public class OrdersDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderPosition> OrderPositions { get; set; }
}

public class Customer
{
public Guid Id { get; set; }
public virtual Order LiveOrder { get; set; }
}

public class Order
{
public Guid Id { get; set; }
public virtual IList<OrderPosition> Positions { get; set; }
public string Name { get; set; }
}

public class OrderPosition
{
public Guid Id { get; set; }
public int Price { get; set; }
}

我需要提供一种方法来更新一些客户的属性以及他的 LiveOrder 和所有 OrderPositions(插入新的、更新现有的和删除旧的)。我尝试了几种方法,但都失败了:

  1. 删除订单和订单头寸,然后插入新的失败
  2. 重复键异常。分离订单并比附加更新 -失败:

Attaching an entity of type 'OrderPosition' failed because another entity of the same type already has the same primary key value.

正确的做法是什么?

演示问题的完整控制台程序:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
public class OrdersDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderPosition> OrderPositions { get; set; }
}

public class Customer
{
public Guid Id { get; set; }
public virtual Order LiveOrder { get; set; }
}

public class Order
{
public Guid Id { get; set; }
public virtual IList<OrderPosition> Positions { get; set; }
public string Name { get; set; }
}

public class OrderPosition
{
public Guid Id { get; set; }
public int Price { get; set; }
}

class Program
{
static void Main(string[] args)
{
var parentId = Guid.NewGuid();
var childId = Guid.NewGuid();

using (var ctx = new OrdersDbContext())
{
var agg = new Customer{
Id = Guid.NewGuid(),
LiveOrder = new Order
{
Id = parentId,
Name = "First order.",
Positions = new[] { new OrderPosition { Id = childId, Price = 3 } }
}};
ctx.Customers.Add(agg);
ctx.SaveChanges();
}

var newParent = new Order
{
Id = parentId,
Name = "Updated order.",
Positions = new[] { new OrderPosition { Id = childId, Price = 5 } }
};
try
{
using (var ctx = new OrdersDbContext())
{
var agg = ctx.Customers.First(x => x.LiveOrder.Id == parentId);

ctx.OrderPositions.RemoveRange(agg.LiveOrder.Positions);
var parent = agg.LiveOrder;
agg.LiveOrder = null;
ctx.Entry(parent).State = EntityState.Detached;
Console.WriteLine(ctx.Entry(parent).State);
ctx.Entry(newParent).State = EntityState.Modified;
agg.LiveOrder = newParent;

ctx.SaveChanges();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}

最佳答案

这完全不是 EF 问题 - 您使用纯 SQL 会遇到同样的问题。

SQL Server 在每次更新时检查唯一性,而不是在提交时检查。作为重新排序临时创建非唯一的......好吧......

您需要 2 次通过。首先将东西移开,然后更新到最终位置。

如何?好吧,SQL Server 数据类型支持负数 ;) 将它们放到它们的位置,负数(即 -4 而不是 4),然后您可以制作一个 SP(或直接 SQL)来为客户反转负数。完成。

但是你需要在更新过程中打破唯一性。

关于c# - 如何更新 Entity Framework 中的大对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com