Entity Framework Code First without app.config

I hope someone can help me because it seems like I'm completely stuck.

For upcoming projects in our company, we would like to use Entity Framework 5 with the first approach to the code. I played for a while, and every time I try to use EF with our existing libraries, I fail because it seems that EF is very dependent on the existing app.config.

Our company has a database library that allows us to connect to various data sources and database technologies, taking advantage of MEF (managed extensibility) for database providers. I just need to pass some database parameters, such as the host (or file), directory, user credentials and the name of the database provider, the library looks for the appropriate plugin and returns me a custom connection string or IDbConnection. We would like to use this library together with EF, because it allows us to be flexible as to which database we use and to modify the database at runtime.

So. I saw that a typical DbContext object does not accept any parameters in the constructor. It automatically searches for the appropriate connection string in app.config. We don’t like such things, so I changed the default constructor to take the DbConnection object, which is passed to the DbContext base class. No deal.

Problems arise when changing the first code model. EF automatically notices this and searches for migration / configuration classes. But: a typical migration class requires a constructor with no default parameters for the context! What a pity!

So, we create our own migration class using the IDbContextFactory interface. But then again, it looks like this IDbContextFactory needs a constructor without parameters, otherwise I cannot add migrations or update the database.

, , , . : , , .

, , EF - app.config. , , app.config !

?

+5
2

fooobar.com/questions/94291/...

, MigrateDatabaseToLatestVersion , :

  • ...
  • ...

DbMigrator , . .

:

public class MasterDetailContext : DbContext
{
    public DbSet<Detail> Detail { get; set; }
    public DbSet<Master> Master { get; set; }

    // this one is used by DbMigrator - I am NOT going to use it in my code
    public MasterDetailContext()
    {
        Database.Initialize( true );
    }

    // rather - I am going to use this, I want dynamic connection strings
    public MasterDetailContext( string ConnectionString ) : base( ConnectionString )
    {
        Database.SetInitializer( new CustomInitializer() );
        Database.Initialize( true );
    }

    protected override void  OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

public class CustomInitializer : IDatabaseInitializer<MasterDetailContext>
{

    #region IDatabaseInitializer<MasterDetailContext> Members

    // fix the problem with MigrateDatabaseToLatestVersion 
    // by copying the connection string FROM the context
    public void InitializeDatabase( MasterDetailContext context )
    {            
        Configuration cfg = new Configuration(); // migration configuration class
        cfg.TargetDatabase = new DbConnectionInfo( context.Database.Connection.ConnectionString, "System.Data.SqlClient" );

        DbMigrator dbMigrator = new DbMigrator( cfg );
        // this will call the parameterless constructor of the datacontext
        // but the connection string from above will be then set on in
        dbMigrator.Update();             
    }

    #endregion
}

:

    static void Main( string[] args )
    {

        using ( MasterDetailContext ctx = new MasterDetailContext( @"Database=ConsoleApplication801;Server=.\SQL2012;Integrated Security=true" ) )
        {
        }

        using ( MasterDetailContext ctx = new MasterDetailContext( @"Database=ConsoleApplication802;Server=.\SQL2012;Integrated Security=true" ) )
        {
        }
    }

.

+3

. , , DbConntectionFactory , :

public DbContext()
{
    IDbContextFactory defaultFactory; //initialize your default here
    DbContext(defaultFactory);
}

public DbContext(IDbContextFactory factory)
{
}
0

All Articles