一次插入或更新多个表的最佳实践是什么 [英] What is the best practice for inserting or updating multiple tables at once

查看:45
本文介绍了一次插入或更新多个表的最佳实践是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要通过一个操作"来更新或插入到多个表中,请调用一个保存信息的示例,其中有多个包含信息"的表.

If I need to update or insert into multiple tables with one "action", example a call to save the information, where there are multiple tables that incorporate 'Information'.

为了论证,可以说我们有下表:

For arguments sake, lets say we have the following tables:

名称地址车职位

每次调用保存信息时,这些表中的每一个都将插入其中.

and with each call to save information, each one of those tables is being inserted into.

哪个更好:

  1. 获取必须写入名称"表的数据.调用InsertOnSubmit并调用SubmitChanges
  2. 获取必须写入地址表的数据.调用InsertOnSubmit并调用SubmitChanges
  3. 获取必须写入Car表的数据.调用InsertOnSubmit并调用SubmitChanges
  4. 获取必须写入作业表的数据.调用InsertOnSubmit并调用SubmitChanges

  1. 获取必须写入名称"表的数据.
  2. 获取必须写入地址表的数据.
  3. 获取必须写入Car表的数据.
  4. 获取必须写入作业表的数据.
  5. 调用InsertOnSubmit,然后调用SubmitChanges.

  1. 获取必须写入名称"表的数据.致电InsertOnSubmit
  2. 获取必须写入地址表的数据.致电InsertOnSubmit
  3. 获取必须写入Car表的数据.致电InsertOnSubmit
  4. 获取必须写入Job表的数据.致电InsertOnSubmit
  5. 致电SubmitChanges

我已经实现了给出的第一个选项,并且我确信这不是最好的选择.我不知道我的网络连接是否有问题,但是有时某些表被写入而其他表没有写入,这确保了我没有尽力而为.

I have implemented the first option given and I am sure this is not the best way to go. I don't know if I have problems with my network connection or whatever, but sometimes some of the tables are written to while others are not and this makes me sure I am not doing it the best way possible.

在一些与此问题类似的帖子中,有一些与视图"和其他选项有关的评论.我没有这方面的经验,因此我需要一些指导以了解执行此操作的最佳方法以及我应该关注的方向.

In some of the posts that are similar to this question, there are comments to do with Views and other options. I have no experience with this, so I need some guidance as to the best way to do this and which direction I should focus my attention on.

推荐答案

为第一个表准备数据并为该表执行InsertInSubmit

Prepare the data for the first table and execute an InsertOnSubmit for the table

然后在第二张,第三张,第四张桌子上也一样.

then the same for the 2nd, 3rd, 4th table.

然后执行SubmitChanges.

then execute the SubmitChanges.

在数据库中,所有行都将被插入(或者,如果包括这些类型的操作,则将更新或删除).如果在数据库更新期间出了点问题,则所有的修改都将被删除(回滚).因此,数据完整性是安全的!

In the database all rows wil be inserted (or updated or deleted if you include those kind of operations). If something goes wrong during the updates of the database ALL of the modifications will be removed (rolled back). So the integrety of the data is secure!

您程序中变量中的数据不会反映数据库,如果发生回滚,您将需要再次访问数据.

The data in the variables in your program will not reflect the database, if a roll back took place, you will need to access the data again.

第二个变体并不总是能很好地工作,因为它只会包括回滚中的最后一个表.如果表1的插入顺利,没有2失败,nr 3和4顺利,那么您将不会注意到第二次失败.

The second variant does not always work well because it will only include the last table in the roll back. if insert of table 1 goes well, no 2 fails, nr 3 and 4 go well, then you will NOT notice the 2nd failure.

这篇关于一次插入或更新多个表的最佳实践是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆