LinqToSQL - 用大量表映射 DataContext?
我一直在从事一个项目,其中使用了涉及很多表的 LinqToSQL
。 所有这些都映射在一个 .dbml
文件中(即只有一个 DataContext
)。 我这样做是假装(目前)您无法跨多个数据上下文加入。 例如...
DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();
var query =
from x in db1.SomeTable
join y in db2.AnotherTable on x.Id equals y.Id
select new
{
x.Column,
y.Column
};
有人认为事实并非如此,我应该将 .dbml
文件分解为单独的较小(即更可管理)的数据上下文。 我现在刚刚设置了一个示例,运行与上面类似的查询并收到以下错误...
base {System.SystemException} = {" 查询包含对项目的引用 在不同的数据上下文中定义。”}
我很好奇,我是否遗漏了什么?有很多需要映射的表的常见做法是什么?如何分解 .dbml 文件?
I've been working on a project where I have been using LinqToSQL
that involved a lot of tables. All of these where mapped in one .dbml
file (i.e. only one DataContext
). I did this on the pretense that (currently) you cant join across multiple data contexts. For example...
DB1DataContext db1 = new DB1DataContext();
DB2DataContext db2 = new DB2DataContext();
var query =
from x in db1.SomeTable
join y in db2.AnotherTable on x.Id equals y.Id
select new
{
x.Column,
y.Column
};
Someone argued that this isn't so, and I should break the .dbml
file down into seperate smaller (i.e more managable) data contexts. I've now just set up an example, run a similar query to the one above and got the following error...
base {System.SystemException} = {"The
query contains references to items
defined on a different data context."}
I am curious, am I missing something? What are the common practices where there are a lot of tables that require mapping? How can you break down a .dbml file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题在这里被问了好几次,但没有明确的答案。 检查以下问题:
一些答案请参阅此博客文章:LINQ to SQL DataContext 的生命周期
This was asked several times here and there is no definite answer. Check this questions:
Some answers refer to this blog post: Lifetime of a LINQ to SQL DataContext