简体   繁体   English

C#WPF从组合框切换数据库上下文

[英]C# WPF Switch db context from combobox

I have a ComboBox object 我有一个ComboBox对象

<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. 使用您的ComboBox示例,我想这就是您想要做的。

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: 然后您的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();
         }
      }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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