Closed
Description
How to handle this kind of T4-modelling problem, when database tables have the same name, but different schemas? The result of the generation process are conflicting (partial) classes.
Environment details
linq2db version: 2.5.2
Database Server: SQL Server 2012
Framework version: .NET Framework 4.6.2
Activity
sdanyliv commentedon Nov 22, 2018
@jdistlr, there are several ways to solve problem:
https://github.com/linq2db/linq2db/tree/master/Source/LinqToDB.Templates#customizing-generation-process
MaceWindu commentedon Nov 22, 2018
But still we need to fix type name clash in generated code
MaceWindu commentedon Nov 23, 2018
Also need to document GenerateSchemaAsType option (and check if we have other undocumented options)
jdistlr commentedon Nov 24, 2018
Thank you so much! GenerateSchemaAsType helps sorting out the conflict.
dipique commentedon Jul 14, 2021
For anyone here because they google searched table name collisions in linq2db T4 template generated code and want to find a solution that doesn't require using schemas as types, here are a couple T4 samples to play with (to be placed between your
LoadMetadata
call andGenerateModel
call):Rename specific table(s) to avoid the conflict(s)
Prepend the schema to the type name to all tables with a table name collision
The first solution involves manually renaming, but has the benefit that if you are adding a table that produces a collision, you can fix that issue without renaming your old table. The second solution automatically detects collisions, but could break existing code that depends on the type name from the old table.
Neither solution is 100% polished. The two issues I'm aware of are: 1) it assumes pluralization instead of checking what setting was specified, and 2) foreign key relationships names will not be updated (but the types will, e.g. your foreign key property will be
public MD_Order Order
instead ofpublic MD_Order MD_Order
); for me, this was preferable, but propagating the change to FK relationships is definitely possible.Hopefully that helps someone with the same issue I had.