如何运行期间要改变LINQ O / RM表的名称/源? [英] How to change LINQ O/R-M table name/source during runtime?

查看:140
本文介绍了如何运行期间要改变LINQ O / RM表的名称/源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,并通过O / R映射器创建一个EntitySet的,使用这一切都与LINQ。

I've got a database, and an entityset created by the O/R-Mapper, using all this with LINQ.

在O / R映射器我需要为每个表,它被用于通过LINQ生成的SQL输入一个表名(源)。在.dbml文件,它看起来是这样的:

In the O/R-Mapper I need to enter a table name (source) for every table, which is being used for the SQL generated by LINQ. In the .dbml file it looks like this:

<Table Name="dbo.Customers" Member="Customers">

现在,我想在运行时更改此表的名称,所以SQL将一些其他的表上运行(customers2008而不是客户,例如)。

Now I'd like to change this table name during runtime, so the SQL will be run against some other table (customers2008 instead of customer, for example).

有没有办法在运行时更改表名(源名称)?

Is there any way to change the table name (source name) during runtime?

[更新]经过一些测试,我沮丧,我不得不发现XmlMappingSource不会使它们不在数据库unaccessable(是的,甚至SqlMetal创建的映射时不顾一切这是不持久),持续计算的属性。

[Update] After some testing to my dismay I had to discover that XmlMappingSource does render computed properties which are not persisted in the database unaccessable (yes, even the mapping created by SqlMetal does ignore everything which is not persisted).

推荐答案

类的......你不得不做这样的事情:

Kind of ... You'd have to do something like this:

  1. 创建设计器中的实体。
  2. 使用SqlMetal生成的.dbml文件的XML映射文件由设计师产生。
  3. 更新在XML文件中的表名。
  4. 然后在你的code,使用接受与XML映射文件中创建一个XmlMappingSource DataContext的构造函数重载。

下面是一个博客文章,解释这个过程中一步一步: <一href="http://weblogs.asp.net/guybarrette/archive/2008/07/23/linq-to-sql-dynamic-mapping.aspx">http://weblogs.asp.net/guybarrette/archive/2008/07/23/linq-to-sql-dynamic-mapping.aspx

Here's a blog post that explains this process step by step: http://weblogs.asp.net/guybarrette/archive/2008/07/23/linq-to-sql-dynamic-mapping.aspx

我是用一个类似的过程与ERP数据库具有表名状ttccom001xxx其中xxx是安装ID(我知道这是一个可怕的模式,但没有什么我可以做这件事)。我们有多个安装,所以我复制了最初的XML映射一次为每个安装,然后用安装ID替换XXX。我写了一个使用正则表达式来照顾的替代一个小型控制台应用程序,并增加一条,作为我的生成过程的一部分。

I'm using a similar process with an ERP database that has table names like ttccom001xxx where xxx is the installation ID (I know it's a horrible schema, but there's nothing I can do about it). We have multiple installations, so I copy the initial xml mapping once for each installation and then replace xxx with the installation ID. I wrote a small console app that uses Regex to take care the replacements and added it as part of my build process.

这篇关于如何运行期间要改变LINQ O / RM表的名称/源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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