gpt4 book ai didi

c# - Linq2Sql : How to handle tables with the same name and different schema names

转载 作者:行者123 更新时间:2023-11-30 18:10:33 25 4
gpt4 key购买 nike

我有一个包含多个表的数据库,这些表具有相同的名称但来自不同的模式。例如:

[DatabaseName].[Schema1].[MyTable]
[DatabaseName].[Schema2].[MyTable]

当 Linq2Sql 为该数据库生成代码时,它似乎只是从第一个模式中选取表并完全忽略第二个模式:

[Table(Name="[Schema1].MyTable")]
public partial class MyTable { }

这实际上使得无法使用 Linq2Sql 查询第二个模式上的表。有解决办法吗?

我的第一个想法是手动编辑生成的代码,以便:

[Table(Name="[Schema1].MyTable")]
public partial class Schema1MyTable { }

[Table(Name="[Schema2].MyTable")]
public partial class Schema2MyTable { }

但是每次数据库更改时都必须维护此代码将是一个巨大的痛苦。还有其他想法吗?

最佳答案

看起来这在 Visual Studio 2010 中不再是问题。我创建了两个表,都命名为 target,并将一个绑定(bind)到架构 a,另一个绑定(bind)到架构 b。我添加了 DBML 并将两个目标表(一个在服务器资源管理器中显示为 target (a),另一个显示为 target (b))拉到设计器中。这创建了一个名为 target 的类和另一个名为 target 1 的类。生成的代码(为清楚起见进行了编辑)显示工具现在生成的类与上面的示例代码非常相似:

[global::System.Data.Linq.Mapping.TableAttribute(Name="a.target")]
public partial class target
{ //... class implementation snipped
}

[global::System.Data.Linq.Mapping.TableAttribute(Name="b.target")]
public partial class target1
{ // ... class implementation snipped
}

关于c# - Linq2Sql : How to handle tables with the same name and different schema names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1245923/

25 4 0