使用 Linq 进行跨数据库连接 - 更新 T4 模板以支持数据库名称?

发布于 2024-11-04 00:02:26 字数 1020 浏览 1 评论 0原文

我当前正在多数据库 SQL Server 环境中运行,并使用 linq to sql 来执行查询。

我正在使用此处记录的方法来实现跨数据库联接: http://www.enderminh.com/blog/archive/2009 /04/25/2654.aspx

基本上:

2 个数据上下文 - 用户和付款

Users.dbo.UserDetails {PK: UserId }
Payments.dbo.CurrentPaymentMethod { PK: UserId }

我将表拖到 DBML 上,然后在属性窗口中,将源更改为dbo.UserDetails 到 Users.dbo.UserDetails 以完全限定数据库名称。

然后,我可以通过执行以下操作来发出单个数据上下文跨数据库连接:

var results = (from user in datacontext.Table<UserDetail>()
        join paymentmethod in dataContext.Table<CurrentPaymentMethod>() on user.UserId equals paymentmethod.UserId
    ... rest of query here ...);

现在这是tickety boo,并且按照我想要的方式工作。我目前遇到的唯一问题是当架构更新等发生时(由于我们处于重要的开发阶段,这种情况相对频繁)。

(最后,问题!) 我想要实现的(并且我已将问题标记为 T4 作为猜测,因为我知道 DBML 文件是 T4 引导的)是当我将任何表拖到源自动拾取的数据上下文上时的自动化方式数据库名称(因此将有 Users.dbo.UserDetails 而不仅仅是 dbo.UserDetails)?

感谢您的指点:)

特里

I'm currently running in a multi-DB SQL Server environment and using linq to sql to perform queries.

I'm using the approach documented here to achieve cross DB joins:
http://www.enderminh.com/blog/archive/2009/04/25/2654.aspx

so basically:

2 data contexts - Users and Payments

Users.dbo.UserDetails {PK: UserId }
Payments.dbo.CurrentPaymentMethod { PK: UserId }

I drag the tables onto the DBML, and in the properties window, change the Source from dbo.UserDetails to Users.dbo.UserDetails to fully qualify the DB name.

I can then issue a single data context cross DB join by doing something like:

var results = (from user in datacontext.Table<UserDetail>()
        join paymentmethod in dataContext.Table<CurrentPaymentMethod>() on user.UserId equals paymentmethod.UserId
    ... rest of query here ...);

Now this is tickety boo and works as I want it to. The only problem I'm currently having is when schema updates etc. happen (which is relatively frequent as we're in a significant dev phase).

(and finally, the question!)
What I want to achieve (and I've marked the question up as T4 as a guess, as I know that the DBML files are T4 guided) is an automated way when I drag any table onto a data context that the Source automatically picks up the DB name (so will have Users.dbo.UserDetails instead of just dbo.UserDetails)?

Thanks for any pointers :)

Terry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2024-11-11 00:02:26

查看 T4 Toolbox 及其提供的 LinqToSql 代码生成器(由 Oleg Sych) - 您可以自定义模板来生成您想要的引用,但我认为您将遇到的问题是数据库名称不存储在 dbml 文件中。

您可能会做的是向生成器添加一个过滤器,也许使用字典或类似的东西,这样在您的 .tt 文件中,您可以维护表及其所属数据库的列表。这样,如果您的维护任务是从设计器中删除该类并再次将其删除,它将获得正确的数据库名称。

Have a look at the T4 Toolbox and the LinqToSql code generator it provides (Courtesy of Oleg Sych) - You can customize the templates to generate references however you'd like, but I think the problem you're going to run into is that the database name isn't stored in the dbml file.

What you could probably do is add a filter to the generator, perhaps using a dictionary or similar, such that in your .tt file, you maintain a list of tables and the databases they belong to. That way, if your maintenance task is to delete the class from the designer and drop it on again, it will get the right database name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文