问题描述
我怎样才能做到这两个场景。
How can I do these 2 scenarios.
目前我在做这样的事情
public class Repository
{
private LinqtoSqlContext dbcontext = new LinqtoSqlContext();
public void Update()
{
// find record
// update record
// save record ( dbcontext.submitChanges()
}
public void Insert()
{
// make a database table object ( ie ProductTable t = new ProductTable() { productname
="something"}
// insert record ( dbcontext.ProductTable.insertOnSubmit())
// dbcontext.submitChanges();
}
}
所以,现在我试图加载XML文件有什么吨的记录。首先,我验证记录一次。然后,我想将其插入到数据库中,但不是每条记录之后做的SubmitChanges()我想要做一团提交底。
So now I am trying to load an XML file what has tons of records. First I validate the records one at a time. I then want to insert them into the database but instead of doing submitChanges() after each record I want to do a mass submit at the end.
所以,我有这样的事情
public class Repository
{
private LinqtoSqlContext dbcontext = new LinqtoSqlContext();
public void Update()
{
// find record
// update record
}
public void Insert()
{
// make a database table object ( ie ProductTable t = new ProductTable() { productname
="something"}
// insert record ( dbcontext.ProductTable.insertOnSubmit())
}
public void SaveToDb()
{
dbcontext.submitChanges();
}
}
然后在我的业务层,我会做像
Then in my service layer I would do like
for(int i = 0; i < 100; i++)
{
validate();
if(valid == true)
{
update();
insert()
}
}
SaveToDb();
所以pretend我的for循环是对所有在XML文件中的记录计数。我第一次验证。如果有效的话,我有我才插入记录更新表。然后,我插入记录。
So pretend my for loop is has a count for all the record found in the xml file. I first validate it. If valid then I have to update a table before I insert the record. I then insert the record.
之后,我要救一切一气呵成。
After that I want to save everything in one go.
我不知道如果我能做到,如果有必须每次都还是什么以后的更新时,质量保存。
I am not sure if I can do a mass save when updating of if that has to be after every time or what.
但我认为它会工作肯定的插入之一。
But I thought it would work for sure for the insert one.
似乎没有任何崩溃,我不知道如何检查记录被添加到的DbContext。
Nothing seems to crash and I am not sure how to check if the records are being added to the dbcontext.
推荐答案
简单的回答是:你不知道。 LINQ2SQL是很多东西 - 它不是一个替代品批量上传/批量复制。你会使用ETL路线多出很多有效的:
The simple answer is: you do not. Linq2Sql is a lot of things - it is not a replacement for bulk upload / bulk copy. You will be a LOT more efficient using the ETL route:
- 生成平面文件(CSV等)的新数据
- 在使用批量加载机制加载到数据库
- 如果数据更新等 - 将其加载到临时表和使用MERGE命令将其合并到主表 。
LINQ2SQL将设计总是吸在大规模插入的情况。 ORM只是不ETL工具。
Linq2Sql will by design always suck in mass insert scenarios. ORM's just are not ETL tools.
这篇关于如何大规模插入/更新中的LINQ to SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!