mehdime / DbContextScope

A simple and flexible way to manage your Entity Framework DbContext instances
http://mehdi.me/ambient-dbcontext-in-ef6/
MIT License
633 stars 271 forks source link

Having a Default DbContextFactory #34

Open reader-man opened 8 years ago

reader-man commented 8 years ago

Maybe it would be nice to have a Default DbContextFactory, that gives the client the possibility to set the connection string for projects that have multiple DBs.

namespace Mehdime.Entity
{
    public class DbContextFactoryDefault : IDbContextFactory
    {
        string _connection;
        public DbContextFactoryDefault(string connection_)
        {
            _connection = connection_;
        }

        public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
        {
            object[] arguments = new object[1] { (object)_connection };

            return (TDbContext)Activator.CreateInstance(typeof(TDbContext), arguments);
        }
    }
}

Use:

Dim dbContextScopeFactory = New DbContextScopeFactory(New DbContextFactoryDefault("testCon"))
Dim ambientDbContextLocator = New AmbientDbContextLocator()

Regards.