使用 SubmitChanges 创建 LINQ 事务

发布于 2024-12-03 12:55:05 字数 694 浏览 3 评论 0原文

因此,我尝试使用 LINQ to SQL 进行事务处理。我读到,如果我使用 SubmitChanges(),它将创建一个事务并执行所有内容,如果出现异常,所有内容都会回滚。我需要使用多个 SubmitChanges() 吗?我正在使用类似此代码的代码,但它不起作用,因为它没有在第一个表上保存任何数据..(我需要它的子表的 ID)。

如果我确实在第一个 InsertOnSubmit 之后使用另一个 SubmitChanges() ,它不会失去事务的概念吗?

myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;

//Set to insert

db.Process.InsertOnSubmit(openProcess);

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process_Id = openProcess.Id;

//Submit all changes at once?

db.SubmitChanges();

So, I'm trying to make a transaction with LINQ to SQL. I read that if I use SubmitChanges(), it would create a transaction and execute everything and in case of an exception everything would be rolled back. Do I need to use MULTIPLE SubmitChanges()? I'm using something like this code and it isn't working because it isn't saving any data on the first table.. (I need it's ID for the children table).

If I do use another SubmitChanges() right after the first InsertOnSubmit doesn't it lose the idea of a transaction?

myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;

//Set to insert

db.Process.InsertOnSubmit(openProcess);

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process_Id = openProcess.Id;

//Submit all changes at once?

db.SubmitChanges();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-12-10 12:55:05

您可以使用 TransactionScope 使整个事情成为事务性的,例如,

using (TransactionScope scope = new TransactionScope())
{
myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;



db.Process.InsertOnSubmit(openProcess);
db.SubmitChanges();
//openProcess.Id will be populated

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process_Id = openProcess.Id;    

db.Products.InsertOnSubmit(product); // I assume you missed this step in your example
db.SubmitChanges();

scope.Complete()
}

如果在调用 scope.Complete() 之前抛出异常,那么整个事情将被回滚。

You can make the whole thing transaciotnal using a TransactionScope e.g.

using (TransactionScope scope = new TransactionScope())
{
myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;



db.Process.InsertOnSubmit(openProcess);
db.SubmitChanges();
//openProcess.Id will be populated

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process_Id = openProcess.Id;    

db.Products.InsertOnSubmit(product); // I assume you missed this step in your example
db.SubmitChanges();

scope.Complete()
}

If an exception is thrown before scope.Complete() is called then the whole thing will be rolled back.

微暖i 2024-12-10 12:55:05

问题是,当更改提交到数据库时,进程的 ID 就被设置了。因为您在将流程 ID 分配给产品的行 (product.Process_Id = openProcess.Id;) 之后提交更改。

正确的方法是使用从 PRODUCT 到 PROCESS 的外键正确设置数据库,并使用 Product 上的导航属性 Process 将流程分配给产品。

代码如下所示:

myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process = openProcess;

db.Product.InsertOnSubmit(product);
db.SubmitChanges();

由于 ProcessProduct 的导航属性,因此您不需要插入 Process。它将自动插入,因为您插入了“父级” - Product

The problem is, that the ID of your process is set when the changes are submitted to the database. Because you submit the changes after the line where you assign the process id to the product (product.Process_Id = openProcess.Id;).

The correct way to do this would be to correctly setup your database with a foreign key from PRODUCT to PROCESS and use the navigation property Process on Product to assign the process to the product.

The code would look like this:

myDataContext db = new myDataContext();

Process openProcess = new Process();

openProcess.Creation = DateTime.Now;
openProcess.Number = pNumber;

Product product = new Product();

product.Code = pCode;
product.Name = pName;
product.Process = openProcess;

db.Product.InsertOnSubmit(product);
db.SubmitChanges();

Because Process is a navigation property of Product, you don't need to insert the Process. It will be inserted automatically, because you insert the "parent" - the Product.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文