实体框架中的线程 [英] Threads in Entity Framework

查看:80
本文介绍了实体框架中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,



如何在实体框架中使用Thread?

这是一个例子:



我在表中插入10,000条记录



Hi Team,

How to use Thread in Entity Framework?
Here is an example:

I am inserting 10,000 Records into Table

tblProduct objProduct=new tblProduct();
for(int i =0; i<10000; i++)
{
objProduct=new tblProduct();
objProduct.Name="Product Name Here"+i;
objDataContaxt.tblProduct.InsertOnSubmit(objProduct);
objDataContaxt.SubmitChanges();
}





这里现在我要为这10,000条记录运行5个流程。

ie。我需要同时插入5个记录。

第一个进程将插入1到200个记录

第二个进程将插入201到400个记录

第3个过程将插入301到600个记录。等等



我需要同时运行所有流程。



可以任何人告诉我怎么可能(如果线程是解决方案,那么线程如何与我上面提到的代码兼容。)



谢谢



Here Now i want run 5 Process for these 10,000 Records.
ie. I need to insert 5 Records at the same time.
1st process will insert 1 to 200 Records
2nd process will insert 201 to 400 Records
3rd process will insert 301 to 600 Records. etc.

I need to run all the process''s at the same time.

Can anybody tell me how is it possible (if thread is the solution then how thread is compatible with my code which is mentioned above.)

Thanks

推荐答案

EF似乎是错误的技术。据推测,您要么将数据库初始化为一次性,要么执行某种导出导入以将数据从另一个源传输到数据库中。无论哪种方式,最好将其作为直接SQL脚本完成,并在不通过EF的情况下针对数据库运行。
EF seems like the wrong technology here. Presumably you are either initialising a database as a one-off, or performing some kind of export-import to transfer data from another source into your database. Either way, it is better done as a direct SQL script and run against the database without going through EF.


我完全赞同BobJanova的解决方案1,但如果我没有,我会感到不舒服这里不提供更多信息。

使用EF时没有理由不能将这项工作并行,前提是你不依赖于自动生成主键的行。特定顺序,因为并行进程不会发生这种情况。我自己没有尝试过,但只要你为每个进程都有一个单独的存储库实例,他们就应该高兴地彼此独立工作。这里需要注意的是每次添加调用后都不要调用SaveChanges,因为这只会导致你在数据库上创建死锁。

注意:我不建议你这样做。这只是关于能够多线程这样的东西的信息。
I entirely agree with Solution 1 by BobJanova, but would feel amiss if I didn''t offer a little more information here.
With EF there is no reason why you can''t parallel this work out provided you aren''t dependent on the rows having auto generated primary keys in a specific order because that will not happen with parallel processes. I haven''t tried this myself, but as long as you have a separate repository instance for each process they should happily work independently of each other. The caveat here is not to call SaveChanges after every add call because this will just cause you to create deadlocks on your database.
NOTE: I DO NOT SUGGEST YOU DO IT THIS WAY. This is just information as to the potential of being able to multi-thread something like this.


这篇关于实体框架中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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