简体   繁体   中英

C# WPF Switch db context from combobox

I have a ComboBox object

<ComboBox Name="Environment">
<ComboBoxItem Content="Development"/>
<ComboBoxItem Content="Production"/>
</ComboBox> 

I would like to be able to switch the models that I use based on this combo box

namespace MyNamespace
{
    public partial class MainWindow : Window
    {
        object entity;
        private void LoadTables()
        {
            if (Environment.Text == "Production")
            {
                entity = new Entities1();
            }
            if (Environment.Text == "Development")
            {
                entity = new Entities2();
            }
            LoadTable1(); 
         }         
         private void LoadTable1()
         {
             // cannot use entity here
             entity.someTable.ToList();
         }
      }
}

The problem is that I can not pass the entity object to the methods so that I can make the database requests.

I have tried using Interfaces but I am not really sure how to implement them. I have this but not sure if its correct or how to use them any help would be greatly appreciated.

    interface iMyDev
    { 
        Entities2 entity { get; set; }            
    }
    interface iMyProd
    {
        Entities1 entity { get; set; }
    }
    class MyBass:iMyDev,iMyProd
    {
       // ????
    }

Using your ComboBox example, I think this is what you'd want to do.

First, define your interface and classes that implement it:

interface IEntity
{
  IEnumerable<object> SomeTable { get; }
}

class DevEntity : IEntity
{
  ...
}

class ProdEntity : IEntity
{
  ...
}

Then your Window will look like this:

namespace MyNamespace
{
    public partial class MainWindow : Window
    {
        IEntity entity;
        private void LoadTables()
        {
            if (Environment.Text == "Production")
            {
                entity = new ProdEntity();
            }
            else if (Environment.Text == "Development")
            {
                entity = new DevEntity();
            }

            LoadTable1(); 
         }

         private void LoadTable1()
         {
             entity.SomeTable.ToList();
         }
      }
}

i ended up creating an interface class moved all properties out of the model classes and into my interface changed both model classes to inherit the interface add the interface properties to the model classes then changed the context of the depending on the combobox

namespace myNamespace
{
    public partial class MainWindow : Window
    {
        public IDBContext context { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            LoadTables()
        }

        private void LoadTables()
        {
            if(cbEnvironment.Text == "Production")
            {
                context = new Entities1(); 
            }
            else 
            {
                context = new Entities2();
            }
        }

    }
    public interface IDBContext
    {
        IDbSet<Table1> Table1 { get; set; }
        IDbSet<Table2> Table2 { get; set; }        
    }

}

namespace myNamespace
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class Entities1 : DbContext, IBassContext
    {
        public Entities1()
            : base("name=Entities1")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual IDbSet<Table1> Table1 { get; set; }
        public virtual IDbSet<Table2> Table1 { get; set; }
    }
}

namespace myNamespace
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class Entities2 : DbContext, IBassContext
    {
        public Entities2()
            : base("name=Entities2")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual IDbSet<Table1> Table1 { get; set; }
        public virtual IDbSet<Table2> Table1 { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM