C#WPF从组合框切换db上下文

时间:2016-01-19 16:48:44

标签: c# wpf combobox

我有一个ComboBox对象

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

我希望能够根据此组合框切换我使用的模型

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();
         }
      }
}

问题是我无法将实体对象传递给方法,以便我可以发出数据库请求。

我尝试过使用Interfaces,但我不确定如何实现它们。我有这个,但不确定它是否正确或如何使用它们任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:0)

使用您的ComboBox示例,我认为这是您想要做的事情。

首先,定义实现它的接口和类:

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

class DevEntity : IEntity
{
  ...
}

class ProdEntity : IEntity
{
  ...
}

然后您的Window将如下所示:

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();
         }
      }
}

答案 1 :(得分:0)

我最终创建了一个接口类 将所有属性移出模型类并进入我的界面 更改了两个模型类以继承接口 将接口属性添加到模型类 然后根据组合框改变了上下文

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; }
    }
}