简体   繁体   English

首先自定义连接字符串EF5,DB

[英]Custom connection string EF5, DB first

So I've generated the table I need, and I need the ability to change my connection string when my program is started. 因此,我已经生成了所需的表,并且需要能够在程序启动时更改连接字符串。 At the moment my connection is 目前我的联系是

"metadata=res://*/entityframework.Model1.csdl|res://*/entityframework.Model1.ssdl
|res://*/entityframework.Model1.msl;provider=MySql.Data.MySqlClient;
provider connection string="server=localhost;User Id=myuserid;
password=12345678;database=databasename""

What I have so far 到目前为止我有什么

Get.designer.cs file: Get.designer.cs文件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.EntityClient;
using Npgsql;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Validation;
using patientlist.entityframework;
using System.Xml;

namespace patientlist
{
    public partial class Get : Form
    {
        Timer update = new Timer();//60000 = 1min
        public Get()
        {
            InitializeComponent();
        } ....

    private void Timer(object sender, EventArgs e)
    {
        string connectionString = "metadata=res://*/entityframework.Model1.csdl|res://*/entityframework.Model1.ssdl|res://*/entityframework.Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;User Id=myuserid;password=12345678;database=databasename"";

        using (var blah = new ltcsystemEntities())
        {
            blah.Database.Connection.ConnectionString = connectionString;
        } .....

I'm using EF5, and it's DB first (I generated some automated code from my entity model) 我使用的是EF5,首先是数据库(我从实体模型生成了一些自动化代码)

With DB First your derived DbContext would have been auto generated for you as a partial class. 使用DB First,您的派生DbContext将作为DbContext类自动生成。

public partial class MyContext : DbContext
{
   public MyContext()
   : base("name=MyContext")
   {
   }

//...
}

Notice how it includes a parameterless constructor which calls the parent constructor of method signature: public DbContext(string nameOrConnectionString) ; 注意,它如何包含一个无参数的构造函数,该构造函数调用方法签名的父构造函数: public DbContext(string nameOrConnectionString) ; passing the name of the connection string. 传递连接字符串的名称。 This is the connection string that should be in your app.config. 这是应该在app.config中的连接字符串。

If you need to change connection strings you can write a partial class to complement the autogenerated class and provide a constructor that accepts the name of another app.config connection string or a connection string itself and then pass this to the parent constructor. 如果需要更改连接字符串,则可以编写局部类以补充自动生成的类,并提供一个接受另一个app.config连接字符串或连接字符串本身名称的构造函数,然后将其传递给父构造函数。

public partial class MyContext
{
   public MyContext(String nameOrConnectionString)
   : base(nameOrConnectionString)
   {
   }
}

You could then use as follows. 然后你可以使用如下。

using(MyContext context = new MyContext(nameOrConnectionString))
{
  //Do stuff
}

However if you are switching connection strings based on some runtime value(s) then you may find it useful to create a factory class to handle the instantiation of your DbContext . 但是,如果您根据某些运行时值切换连接字符串,那么您可能会发现创建工厂类来处理DbContext的实例化DbContext

refer Web.config on the root directory of the project. 在项目的根目录中引用Web.config。

Your connection strings are located within 'connectionStrings' tag 您的连接字符串位于“connectionStrings”标记内

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

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