Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Entity Framework 6 - Set Connection String at Runtime

Entity Framework 6 - Set Connection String at Runtime

Scheduled Pinned Locked Moved Database
csharpcomhelptutorialquestion
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I created an EF6 data context by following [this](http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx). I need to change the connection string at runtime. I found [this article](https://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at) but my data context does not have an overloaded contstructer:

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

    What am I doing wrong? How to I set the EF data Context connection string at runtime??

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I created an EF6 data context by following [this](http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx). I need to change the connection string at runtime. I found [this article](https://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at) but my data context does not have an overloaded contstructer:

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

      What am I doing wrong? How to I set the EF data Context connection string at runtime??

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Add the required constructor overloads to your context:

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

      public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
      {
      }
      
      public MyEntities(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection)
      {
      }
      

      EDIT: If you're using the .tt file to generate the context from the database, you'll probably need to add the extra overloads in a separate file; that's why the class is declared as partial.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups