Page 1 of 1

Ignoring Tables in mapping

Posted: Mon 10 May 2021 20:25
by rakeshn
Hi,
I have to support 2 different schema versions of a database. The new version has 2 extra tables.
Is it possible to use same DBContext object but with 2 different mappings?

This is what I have:

Code: Select all

public class MyDataContext: DataContext
{
    public Table<Table1> Table1;
    public Table<Table2> Table2;

    public MyDataContext(string connectionString, bool isNewSchema): base( connectionString, isNewSchema? MyMapping.NewMappingSource: MyMapping.LegacyMappingSource)
    {
    }  
}

public static class MyMapping
{
    public MappingSource NewMappingSource 
   {
       get {
            var mySqlDataProvider = new MySqlDataProvider();
            var builder = new FluentMappingBuilder(mySqlDataProvider);      
            var entity1 = builder.Entity<Table1>;
              // Do entity1 column mappings            
            var entity2 = builder.Entity<Table2>;
              // do entity2 column mappings
      }
   }

    public MappingSource LegacyMappingSource 
   {
       get {
            var mySqlDataProvider = new MySqlDataProvider();
            var builder = new FluentMappingBuilder(mySqlDataProvider);      
            var entity1 = builder.Entity<Table1>;
              // Do entity1 column mappings            
            builder.Ignore<Table2>;
      }
   }
}
`Ignore` doesnt seem to be working. DataContext construtor throws an exception

"System.InvalidOperationException
Unable to resolve root for type Table2"

Anyway around this? I am trying to avoid creating a new DataContext. Appreciate the help.

Re: Ignoring Tables in mapping

Posted: Wed 12 May 2021 18:54
by Shalex
Here is an example of how to use different mappings with the same context: viewtopic.php?f=31&t=35460 (1st approach).