WildFlower
WildFlower

Reputation: 129

Pass connection string to EF DbContext code first

I have a context that derives from DbContext like the one below:

public class StudentContext : DbContext
{
public StudentContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DBModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentContext, StudentMigrations.Configuration>());
}

public DbSet<Students> Students {get; set;}
}

I'm trying to pass the connection string by:

studentContext = new StudentContext(settings.ConnectionString)

The settings are loaded at run-time by reading a configuration file. I've tried this and I've also tried setting the connection string inside the StudentContext constructor by using this.Database.Connection.ConnectionString.In either case, I get an exception that asks me to provide a default constructor or provide an implementation of IDbContextFactory. The only thing that works is this:

public class StudentContext : DbContext
{
  public static string ConnectionString;
public StudentContext(string connectionString) : base(ConnectionString = connectionString)
{
}

//And also provide a default implementation of the DbContext constructor:
public StudentContext() : base(ConnectionString)
{
}

protected override void OnModelCreating(DBModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentContext, StudentMigrations.Configuration>());
}

public DbSet<Students> Students {get; set;}
}

I am trying to reduce the use of statics in code and therefore, if I could get the first option to work, that'd be great.

Upvotes: 0

Views: 2439

Answers (2)

Nishant Kumar Verma
Nishant Kumar Verma

Reputation: 442

We have to specify the entity connection string. in DbContext

  SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
        {
    DataSource = "SOURAV-PC", // Server name
    InitialCatalog = "efDB",  //Database
            UserID = "sourav",         //Username
            Password = "mypassword",  //Password
        };
        //Build an Entity Framework connection string

        EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
        {
            Provider = "System.Data.SqlClient",
            Metadata =   "res://*/testModel.csdl|res://*/testModel.ssdl|res://*/testModel.msl",
            ProviderConnectionString = sqlString.ToString()
        };
        return entityString.ConnectionString;
    }

Upvotes: 0

WildFlower
WildFlower

Reputation: 129

Turns out that the connection string is cached in a readonly string for MigrateDatabaseToLatestVersion from this answer. I just had to update the class to:

public class StudentContext : DbContext
{
    public StudentContext(string connectionString) : base(connectionString)
    {
    }

    protected override void OnModelCreating(DBModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentContext, StudentMigrations.Configuration>(true)); //Passing true here to reuse the client context triggering the migration 
    }

    public DbSet<Student> Students {get; set;} 
}

Upvotes: 2

Related Questions