如何复制一行数据链接到其他表(相关)并将它们作为新记录插入 - Linq C#
How to copy a row with his data linked in other tables (related ) and insert them as a new records - Linq C#
如何复制与其他 table 中链接的数据的行(相关)并将它们作为新记录插入所有表中,使用 :
ASP.NET MVC/C#/Linq(方法语法)/Entity Framework代码优先
例如:
我有这些 tables =>
我想复制"Customer"的最后一行table并在数据库的新行中添加一个新的ID,同时复制"Customer"中所有相关的订单table "Order" 并使用新 ID 的 "Customer" 的 "CustoemerID" 复制它们,同时复制 table [=41= 中的所有订单行] 并用新的 "OrderID"
复制它们
怎么做?
是否有执行此操作的简短代码?
The blue color is the last row in the database, and the green color is
the result of the code what i'm looking for, a duplicate copy of the
customer and his related data
主键:CustomerID & OrderID & OrderLineID = AUTO INCREMENT Field
看看这是否对你有帮助:
INSERT INTO customer (ID, Name, Address1, Address2, Address3)
SELECT
3, Name, Address1, Address2, Address3
FROM
customer
WHERE
ID = 2;
INSERT INTO Order (CustomerId, TotalAmount)
SELECT CustomerId, TotalAmount
FROM Order
WHERE CustomerID = 3;
INSERT INTO OrderLine (OrderId, ProductId, Quantity)
SELECT ol.OrderId, ol.ProductId, ol.Quantity
FROM OrderLine ol
WHERE ol.OrderId IN (SELECT o2.OrderId
FROM Order o2
WHERE o2.CustomerID = 3);
这个问题我能想出2种解法
使用Sql查询
首先使用 EF 代码,您可以在客户内部创建一个克隆方法 class。
CREATE TABLE #Customer (
CustomerId INT Identity(1, 1)
,Name NVARCHAR(100)
,Address1 NVARCHAR(100)
,Address2 NVARCHAR(100)
,Address3 NVARCHAR(100)
)
INSERT INTO #Customer
VALUES (
'N2'
,'A11'
,'A22'
,'A33'
)
INSERT INTO #Customer
SELECT Name
,Address1
,Address2
,Address3
FROM #Customer
GROUP BY Name
,Address1
,Address2
,Address3
HAVING Count(*) BETWEEN 0
AND 1
使用克隆方法
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public Customer Clone()
{
// where the reference is not detached
var employee1 = new Customer
{
Name = Name,
Address1 = Address1,
Address2 = Address2,
Address3 = Address3
};
// with reference detached
var serialisedData = JsonConvert.SerializeObject(this);
var employee2 = JsonConvert.DeserializeObject<Employee>(serialisedData);
// return employee1 or employee2;
return employee1;
}
}
如何复制与其他 table 中链接的数据的行(相关)并将它们作为新记录插入所有表中,使用 :
ASP.NET MVC/C#/Linq(方法语法)/Entity Framework代码优先
例如:
我有这些 tables =>
我想复制"Customer"的最后一行table并在数据库的新行中添加一个新的ID,同时复制"Customer"中所有相关的订单table "Order" 并使用新 ID 的 "Customer" 的 "CustoemerID" 复制它们,同时复制 table [=41= 中的所有订单行] 并用新的 "OrderID"
复制它们怎么做?
是否有执行此操作的简短代码?
The blue color is the last row in the database, and the green color is the result of the code what i'm looking for, a duplicate copy of the customer and his related data
主键:CustomerID & OrderID & OrderLineID = AUTO INCREMENT Field
看看这是否对你有帮助:
INSERT INTO customer (ID, Name, Address1, Address2, Address3)
SELECT
3, Name, Address1, Address2, Address3
FROM
customer
WHERE
ID = 2;
INSERT INTO Order (CustomerId, TotalAmount)
SELECT CustomerId, TotalAmount
FROM Order
WHERE CustomerID = 3;
INSERT INTO OrderLine (OrderId, ProductId, Quantity)
SELECT ol.OrderId, ol.ProductId, ol.Quantity
FROM OrderLine ol
WHERE ol.OrderId IN (SELECT o2.OrderId
FROM Order o2
WHERE o2.CustomerID = 3);
这个问题我能想出2种解法
使用Sql查询
首先使用 EF 代码,您可以在客户内部创建一个克隆方法 class。
CREATE TABLE #Customer ( CustomerId INT Identity(1, 1) ,Name NVARCHAR(100) ,Address1 NVARCHAR(100) ,Address2 NVARCHAR(100) ,Address3 NVARCHAR(100) ) INSERT INTO #Customer VALUES ( 'N2' ,'A11' ,'A22' ,'A33' ) INSERT INTO #Customer SELECT Name ,Address1 ,Address2 ,Address3 FROM #Customer GROUP BY Name ,Address1 ,Address2 ,Address3 HAVING Count(*) BETWEEN 0 AND 1
使用克隆方法
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public Customer Clone()
{
// where the reference is not detached
var employee1 = new Customer
{
Name = Name,
Address1 = Address1,
Address2 = Address2,
Address3 = Address3
};
// with reference detached
var serialisedData = JsonConvert.SerializeObject(this);
var employee2 = JsonConvert.DeserializeObject<Employee>(serialisedData);
// return employee1 or employee2;
return employee1;
}
}