none
Entity Framework model- database first setting connection string at runtime RRS feed

  • Question

  • Hi everybody:

    I have the next situation:

    I made a database first EF 6.0 App, now i want to set the conecction at runtime. Diferent documentation that i has reviewed say that when i made a new instance of the model (MyModelType X = new MyModelType()) i pass the conecction as a parameter, but the compiler (c#) doesn't allow me to do such thing.

    Looking at the code of the model (the class derived from dbContext) doesn't admit the parameter. If i make the change in order to the class admit the parameter when i update the model from databe , the change is overrride. What can i do?

    Advce thks all of you

    Thursday, January 16, 2014 6:06 PM

Answers

  • Hi. You can pass a connection string as a constructor parameter to the DbContext. So when you create your context derived from DbContext just accept a string parameter and pass it on. Then when you are creating an instance of you context you can pass in the connection string.

    public class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {
    
        }
    }


    Friday, January 17, 2014 1:03 AM

All replies

  • Hi. You can pass a connection string as a constructor parameter to the DbContext. So when you create your context derived from DbContext just accept a string parameter and pass it on. Then when you are creating an instance of you context you can pass in the connection string.

    public class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {
    
        }
    }


    Friday, January 17, 2014 1:03 AM
  • Thks J Chase for your answer. Yes  i can do that, but when i update the model from database, this change is overritten.
    Saturday, January 18, 2014 1:43 AM
  • The generated context is a partial class. If you want to make changes which won't get lost you can put them in a separate file. So if you have a generated context myContext you could create a new partial class and declare the constructor in that (under the same namespace).

    public partial class myContext : DbContext
    {
        public myContext(string conn)
            : base(conn)
        {
    
        }
    }

    Sunday, January 19, 2014 4:09 AM
  • Thks J Chase, i will try that, thks very much.

    I'll comment when i do it.

    Sunday, January 26, 2014 12:09 AM
  • Hi again J Chase:

    I do the new file for the partial class, there's no problema, it work, but now i have a new situation:

    I used String concecction builder for sqlConecction and entityframework conection

    when i call the open method of entityconnection, an error about the provider doesn't return a ProviderManifest

    I'm using sqlexpress 2012 and vs2013 for desktop with EF 6.0

    Sunday, February 2, 2014 7:44 AM
  • Hi,

    I would like to understand but I don't.

    This little piece of code you provided, what is it ?  Where do I use it.  From what I understand of the question,  I do not see how this solves the problem. I have the same problem here.

    To get my CustomerEntities context I do CustomerEntities Context = new CustomerEntities() and we can't pass parameter to it and this is what we need to instantiate so having a class derived from DbConext what will this do and what to put in it ? I mean  How do we connect to different database then the one the CustomerEntities will use from app.config ?


    • Edited by GearWorld Wednesday, February 18, 2015 12:50 AM
    Wednesday, February 18, 2015 12:48 AM