Channel Express Filters

SqlDbContextScope (Entity Framework)

The NuGet package https://www.nuget.org/packages/Terralect.ChannelExpress.SqlDbContextScope/ is a Channel Express filter that manages your Entity Framework DbContext on your behalf.


This package is actually leveraging off of the Terralect.ChannelExpress.SqlScope package, which is also a Channel Express filter package. The SqlScope filter automatically creates a SqlConnection and SqlTransaction, which can be accessed by injecting ISqlConnectionScope in a Channel Express filter, handler or scoped class in the Channel.


Once the SqlDbContextScope package is configured, you will inject in an IDbContextScope to gain access your DbSets.


You will not be required to call the DbContext SaveChanges() method, as this is handled automatically in the Channel Express filter.

Setup

The minimum required setup will look as follows:


    ChannelExpressSetup.Configure(channelConfig =>
   {
       channelConfig.ConfigureDependencyRegistrar(new DependencyManager(serviceCollection));   
       channelConfig.DbContextScope<LocalDbContext>(dbContextConfig =>
       {
           dbContextConfig.ConnectionString = "your_connection_string";
           dbContextConfig.SetDbContextProvider(options => new LocalDbContext(options));
       });
   });


Also, your DbContext class should have the following constructor:


    public LocalDbContext(DbContextOptions<LocalDbContext> options) : base(options)
   {
   }

IDbContextScope

Once you have injected IDbContextScope, you will have access to the following methods:


dbContextScope.DbSet<SomeDbSet>()

 * This allows you to access a DbSet directly.


dbContextScope.Queryable<SomeDbSet>()

 * This accesses a DbSet, disables the EF tracking and returns an IQueryable.


dbContextScope.DbContext<YourDbContext>()

 * This gives you direct access to the instance of your DbContext that was created.

Sample Download Code

ChannelExpress2.DbFilters.Demo (zip)

Download