gpt4 book ai didi

c# - 我是否需要为引用主行的每一行调用 InsertOnSubmit?

转载 作者:太空宇宙 更新时间:2023-11-03 22:26:03 24 4
gpt4 key购买 nike

LINQ to SQL - 当插入一行(以及其他几行 FK 将指向该行上的 PK)时,执行 [context].[MainTable].InsertOnSubmit(row) 是否足够,或者我是否其他行也需要调用它吗?

这是一个精简代码示例 (C#):

//Forgive the contrived example (off the top of my head) :)

//Main row
Order order = new Order
{
itemId = (int) data["itemNumber"],
address = (string) data["address"]
};
db.Orders.InsertOnSubmit(order); //would be nice to only have to submit here.

//Related rows
OrderPerson orderPerson = new OrderPerson
{
Order = order,
//other things, etc.
orderRoleId = RoleIds.Customer
};
//Q: need to do "db.OrderPerson.InsertOnSubmit(orderPerson);" here?

OrderHistoryEntry historyEntry = new OrderHistoryEntry
{
Order = order,
//other things, etc.
historyTypeId = HistoryIds.Ordered
};
//Q: need to do "db.OrderHistoryEntry.InsertOnSubmit(historyEntry);" here?

db.SubmitChanges();

执行以下操作是否足够:db.Orders.InsertOnSubmit(order); 还是我还需要为相关行(orderPerson 和 historyEntry)执行 InsertOnSubmit?

最好只在主行运行一次。 MSDN 示例就是这样做的,但它们的关系都是相反的(从主行引用其他行)。

我很欣赏你的想法。

最佳答案

这个怎么样?

Order order = new Order
{
itemId = (int) data["itemNumber"],
address = (string) data["address"]
};

OrderPerson orderPerson = new OrderPerson
{
orderRoleId = RoleIds.Customer
};
order.OrderPersons.Add(orderPerson);

OrderHistoryEntry historyEntry = new OrderHistoryEntry
{
historyTypeId = HistoryIds.Ordered
};
order.OrderHistoryEntries.Add(historyEntry);

db.Orders.InsertOnSubmit(order);

db.SubmitChanges();

关于c# - 我是否需要为引用主行的每一行调用 InsertOnSubmit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1376802/

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