Ignoring Tables in mapping

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
rakeshn
Posts: 1
Joined: Mon 10 May 2021 20:06

Ignoring Tables in mapping

Post by rakeshn » Mon 10 May 2021 20:25

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.

Shalex
Site Admin
Posts: 9302
Joined: Thu 14 Aug 2008 12:44

Re: Ignoring Tables in mapping

Post by Shalex » Wed 12 May 2021 18:54

Here is an example of how to use different mappings with the same context: viewtopic.php?f=31&t=35460 (1st approach).

Post Reply