简体   繁体   中英

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

I have a database with multiple tables which have the same name but are from different schemas. For example:

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

When Linq2Sql generates code for this database, it appears to just be picking up the table from the first schema and completely ignoring the second schema:

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

This effectively makes it impossible to query the table on the second schema using Linq2Sql. Is there a workaround for this?

My first idea is to manually edit the generated code so that I have:

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

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

but having to maintain this code every time the database changes would be a huge pain. Any other ideas?

It looks like this is no longer an issue in Visual Studio 2010. I created two tables, both named target , and bound one to schema a , and the other bound to schema b . I added the DBML and pulled both target tables (one shows as target (a) and the other target (b) in Server Explorer) onto the designer. This created one class named target and another named target 1 . The generated code (edited for clarity) shows the tooling now generates classes much like your example code above:

[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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM