One DataContext to use between 2 identical database

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
anthony888999
Posts: 2
Joined: Sun 28 May 2017 21:52

One DataContext to use between 2 identical database

Post by anthony888999 » Sun 28 May 2017 22:09

I had two identical database with same structure. ( call it dbase1 and dbase2 )
dbase1 is use for real data and dbase2 is use for training , testing purpose.
I would like to just switch between the two database dynamically on my application.

I had follow some generated xml hint at past discussion - one "DataContext"
viewtopic.php?f=31&t=24588#p83144

But, I still end up with duplicate code:

Dbase1:

Stream contextStream = System.IO.File.OpenRead("DataContext1.xml");
Devart.Data.Linq.Mapping.MappingSource mappingSource =
Devart.Data.Linq.Mapping.XmlMappingSource.FromStream(contextStream);

Dbase1DataContext dc = new Dbase1DataContext(iconnect, mappingSource);

var query = from it in dc.Categories
orderby it.Id
select it;
foreach (Dbase1Context.Category cat in query)
textBox.Text += i++.ToString() + " " + cat.Name + "\n";

DBase2:

Stream contextStream = System.IO.File.OpenRead("DataContext2.xml");
Devart.Data.Linq.Mapping.MappingSource mappingSource =
Devart.Data.Linq.Mapping.XmlMappingSource.FromStream(contextStream);

Dbase2DataContext dc = new Dbase2DataContext(iconnect, mappingSource);

var query = from it in dc.Categories
orderby it.Id
select it;
foreach (Dbase2Context.Category cat in query)
textBox.Text += i++.ToString() + " " + cat.Name + "\n";

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

Re: One DataContext to use between 2 identical database

Post by Shalex » Fri 02 Jun 2017 13:10

1st approach (two different sources, e.g. Oracle and MySQL)

Code: Select all

        static void Main(string[] args)
        {
            CommonCode(Dbase1ConnString, Dbase1PathToMappingSource);

            CommonCode(Dbase2ConnString, Dbase2PathToMappingSource);
        }

        static void CommonCode(string connString, string pathToMappingSource)
        {
            var contextStream = System.IO.File.OpenRead(pathToMappingSource);
            var mappingSource = Devart.Data.Linq.Mapping.XmlMappingSource.FromStream(contextStream);

            var dc = new CommonDataContext(connString, mappingSource);

            var query = from it in dc.Categories
                        orderby it.Id
                        select it;
            foreach (CommonDataContext.Category cat in query)
                textBox.Text += i++.ToString() + " " + cat.Name + "\n";
        }
2nd appoach (the same data source, e.g. MySQL in both cases). JIC: if the names of schemas in databases are different, you should clear the schema prefix in the Source attribute of classes (or clear selection of the "Preserve schema name in storage" option in Create Model Wizard)

Code: Select all

        static void Main(string[] args)
        {
            CommonCode(Dbase1ConnString);

            CommonCode(Dbase2ConnString);
        }

        static void CommonCode(string connString)
        {
            var contextStream = System.IO.File.OpenRead(commonMappingSource);
            var mappingSource = Devart.Data.Linq.Mapping.XmlMappingSource.FromStream(contextStream);

            var dc = new CommonDataContext(connString, mappingSource);

            var query = from it in dc.Categories
                        orderby it.Id
                        select it;
            foreach (CommonDataContext.Category cat in query)
                textBox.Text += i++.ToString() + " " + cat.Name + "\n";
        }
    }

Post Reply