0

Ok, this is definitely my last question about Linq2DB! Maybe...

I've gotten through some of the learning curve with Linq2DB for a project that will work against DB2/iSeries data. One problem though is that while my code works against my test database just fine, in production it will need to point at different schemas for the same objects. For instance, a particular user class in one environment would have a table mapping like:

[Table(Schema="ABC", Name="USERS")]

in another environment it might look like:

[Table(Schema="XYZ", Name="USERS")]

I haven't quite figured out how I will approach this in production. Has anyone dealt with this before? Is there a way to do this with a DataContext? Or possibly by digging into the internals of the mapping? Any thoughts or ideas are appreciated!

Matt McNabb
  • 362
  • 4
  • 15

1 Answers1

2

I would recommend to use fluent mapping or configurations for your case.

For fluent mapping pass schema name to fluent mapping builder function:

void ConfigureMappings(MappingSchema ms, string schema)
{
    ms.GetFluentMappingBuilder()
        .Entity<Users>()
            .HasSchemaName(schema)
    // configure columns and other entities
}

Configuration-based solution: use different configurations for test and production environments and pass configuration name to data connection constructor:

[Table(Schema="ABC", Name="USERS", Configuration="test")]
[Table(Schema="XYZ", Name="USERS", Configuration="production")]
public class User
{...}

// or make test configuration default and override it for production where it differ from default
[Table(Schema="ABC", Name="USERS")]
[Table(Schema="XYZ", Name="USERS", Configuration="production")]
public class User
{...}

This approach (with configurations) also could be used for fluent mapper.

  • Thanks! I was able to get this working at a basic level using the fluent mapping approach. This will make it possible to use my library against any arbitrary system where this particular application is installed. – Matt McNabb Dec 30 '18 at 06:42