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

I have a database with several tables that have the same name, but from different schemas. For example:

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

When Linq2Sql generates code for this database, it simply compiles the table from the first schema and completely ignores the second schema:

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

This actually makes it impossible to query the table in 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 to maintain this code every time a database change is a huge pain. Any other ideas?

+3
source share
1 answer

, Visual Studio 2010. target a, b. DBML ( target (a) (b) Server Explorer) . 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
}
+1

Source: https://habr.com/ru/post/1714634/


All Articles