DbContext持久化与静态方法基于控制台应用程序管理员用户选择 [英] DbContext persistence with static method based on console application admin user selection

查看:610
本文介绍了DbContext持久化与静态方法基于控制台应用程序管理员用户选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DbContext 类,只要控制台应用程序正在运行,我只想将适当的环境连接字符串设置一次。

  public class RPMContext:DbContext {
public RPMContext(string nameOrConnectionString)
:base(nameOrConnectionString){
}
}

然后它将被调用

  private RPMContext db = new RPMContext(name = LH_RPMContext); 

然而,我需要持久化一个静态的全局方法,以便我可以依赖于运行控制台应用程序的生命周期不会消失



我正在使用这样的菜单系统

  Console.WriteLine(Select Environment:\\\
);
Console.WriteLine(1。Localhost);
Console.WriteLine(2。DEV);
Console.WriteLine(3。QA);
Console.WriteLine(4。Day 1);
Console.WriteLine(5。Production\\\
);
Console.Write(Number:);
byte environment = byte.Parse(Console.ReadLine());

所以我想如果用户选择了 2 这是DEV,然后

  var dev_db = new RPMContext(name = DEV_RPMContext); 

不过之后,当我使用Entity Framework进行各种CRUD操作时,我将要实例化再次...所以我只想在记忆中保持这个值,对吧?



所以也许一些枚举这些值如 DEV_RPMContext 然后设置它?



这是我的另一个问题:将字符串传递给具有Base类的DbContext的构造方法未设置值

解决方案

一个选项是编辑您的DbContext模板,如下所示:

  public class RPMContext:DbContext {
public static string DefaultConnectionString;
public RPMContext():base(DefaultConnectionString){
}
public RPMContext(string nameOrConnectionString)
:base(nameOrConnectionString){
}
}

然后当选择时,分配所选的连接字符串:

  RPMContext.DefaultConnectionString =你的连接字符串; 

然后只是像往常一样实例化上下文:

  using(var ctx = new RPMContext()){
//在此使用默认连接字符串
}
/ pre>

I have a DbContext class in which I want to set the proper environment connection string only once as long as the console application is running.

public class RPMContext : DbContext {    
    public RPMContext(string nameOrConnectionString)
        :base(nameOrConnectionString) {
    }  
}

Then it would be called up with

 private RPMContext db = new RPMContext("name=LH_RPMContext");

However I need to persist a static global method of this so that I can count on the persistence of the lifetime of the running console application that it does not go away

I was having a menu system like this

 Console.WriteLine("Select Environment:\n");
 Console.WriteLine("1. Localhost");
 Console.WriteLine("2. DEV");
 Console.WriteLine("3. QA");
 Console.WriteLine("4. Day 1");
 Console.WriteLine("5. Production\n");
 Console.Write("Number: ");
 byte environment = byte.Parse(Console.ReadLine());

So I suppose that if the user picked 2 which is "DEV" then

 var dev_db = new RPMContext("name=DEV_RPMContext");

However later on when I am doing all sorts of CRUD operations with Entity Framework, I will want to instantiate it again ... so thus I will just want to hold that value in memory then, right?

So maybe some enum with these values like DEV_RPMContext and then set it ?

Here was my other question: Passing string into a Constructor of DbContext with a Base class is not setting the value

解决方案

One option is to edit your DbContext template like this:

public class RPMContext : DbContext {
    public static string DefaultConnectionString;
    public RPMContext():base(DefaultConnectionString) {
    }   
    public RPMContext(string nameOrConnectionString)
        :base(nameOrConnectionString) {
    }  
}

Then when choice is made, assign selected connection string:

RPMContext.DefaultConnectionString = "your connection string";

And then just instantiate context as usual:

using (var ctx = new RPMContext()) {
// uses default connection string here
}

这篇关于DbContext持久化与静态方法基于控制台应用程序管理员用户选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆