DbContextScope icon indicating copy to clipboard operation
DbContextScope copied to clipboard

Having a Default DbContextFactory

Open reader-man opened this issue 9 years ago • 0 comments

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.

reader-man avatar Jan 04 '16 01:01 reader-man