为什么我的数据没有从一个表插入另一个表 [英] Why my data is not inserting from one table to another

查看:61
本文介绍了为什么我的数据没有从一个表插入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的数据没有使用linq从一个表插入另一个表,它在dataclass.savechanges()显示错误,显示不允许新事务becoz有正在运行。



sunelikaEntities dataClass = new sunelikaEntities();



var str = from dataClass.invoices中的a选择a;



cashpundit ls = new cashpundit();



foreach(str中的var val)

{



ls.invno = val.invno;

ls.customer = val.customer;

ls.amount = val .amount;

ls.date_of_invoice = val.date_of_invoice;

ls.invoice_due_date = val.invoice_due_date;

ls.modified_date = val.modified_date ;

ls.data_entry_date = val.data_entry_date;





dataClas s.cashpundits.AddObject(ls);

dataClass.SaveChanges();



我尝试过:



foreach(str.tolist()中的var val;

why my data is not inserting from one table to another using linq it is showing error at dataclass.savechanges(),showing new transaction is not allowed becoz there are thre running.

sunelikaEntities dataClass = new sunelikaEntities();

var str = from a in dataClass.invoices select a;

cashpundit ls = new cashpundit();

foreach (var val in str)
{

ls.invno = val.invno;
ls.customer = val.customer;
ls.amount = val.amount;
ls.date_of_invoice = val.date_of_invoice;
ls.invoice_due_date = val.invoice_due_date;
ls.modified_date = val.modified_date;
ls.data_entry_date = val.data_entry_date;


dataClass.cashpundits.AddObject(ls);
dataClass.SaveChanges();

What I have tried:

foreach (var val in str.tolist());

推荐答案

请点击以下链接了解如何解决您的问题:

c# - 来自实体框架的SqlException - 不允许新事务,因为会话中还有其他线程运行 - Stack Overflow [ ^ ]

c# - 新交易是不允许的,因为会话中还有其他线程在运行LINQ To Entity - 堆栈溢出 [ ^ ]



注意:魔法会做 ToList()方法;)

Follow below links to find out how to resolve your issue:
c# - SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session - Stack Overflow[^]
c# - New transaction is not allowed because there are other threads running in the session LINQ To Entity - Stack Overflow[^]

Note: the magic will do ToList() method ;)
var str = (from a in dataClass.invoices select a).ToList();



或 - 不同的解决方案:


or - different solution:

foreach (var val in str.ToList())



或 - 另一种解决方案:


or - another solution:

IList str = from a in dataClass.invoices select a;
//then
foreach(YourType val in str)
//replace YourType with class which represent object to enumerate


这篇关于为什么我的数据没有从一个表插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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