如何使用linq将同一对象插入多个dbs [英] How to insert same object to multiple dbs using linq

查看:94
本文介绍了如何使用linq将同一对象插入多个dbs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用linq

将行插入多个数据库但是在提交第一个数据库后我无法将行插入另一个数据库

我想要制作dbs具有相同ID的相同数据



我尝试过:



I have to insert rows to multiple databases using linq
but after submit the first db i can't insert the row to another db
I want to make the dbs have the same data with same ids

What I have tried:

City TheRec1 = new City();
TheRec1.Name = c.Name;

DB1.Cities.InsertOnSubmit(TheRec1);
DB1.SubmitChanges();
DB2.Cities.InsertOnSubmit(TheRec1);//I get error here
DB2.SubmitChanges();
DB3.Cities.InsertOnSubmit(TheRec1);
DB3.SubmitChanges();
DB4.Cities.InsertOnSubmit(TheRec1);
DB4.SubmitChanges();
DB5.Cities.InsertOnSubmit(TheRec1);
DB5.SubmitChanges();

推荐答案

您不能将单个实体对象附加到多个上下文,并让所有这些上下文相互协作。他们根本不了解彼此。



使这项工作的唯一方法是为每个上下文提供每个实体对象的一个​​副本。在您的示例中,您需要五个TheRec1对象副本,每个副本对应一个与您的每个数据库一起使用的每个上下文。
You cannot attach a single entity object to multiple contexts and have all those contexts cooperate with each other. They don't know about each other at all.

The only way to make this work is to have one copy of each entity object for each context. In your example, you need five copies of your "TheRec1" object, one for each context that is working with each of your databases.


这篇关于如何使用linq将同一对象插入多个dbs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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