2

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:

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)

3
  • databasename&quot &quot? Commented Jul 3, 2013 at 21:10
  • that's what was generated using EF Commented Jul 3, 2013 at 21:11
  • I just need to make it so that multiple users can login to the same database (via my form) Commented Jul 3, 2013 at 21:12

2 Answers 2

7

With DB First your derived DbContext would have been auto generated for you as a partial class.

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); passing the name of the connection string. This is the connection string that should be in your 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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, you will also find issues when you get to migrations unless the default (parameterless) constructor is the one to migrate. It is possible but requires more code
0

refer Web.config on the root directory of the project.

Your connection strings are located within 'connectionStrings' tag

1 Comment

I only have an app.config, and I need to be able to change it when my program starts up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.