Paste DbContext with Autofac

I have the following EntityFramework context:

public class Context : DbContext, IDbContext { } 

Where the IDbContext is as follows:

 public interface IDbContext { DbEntityEntry Entry(Object entity); IEnumerable<DbEntityValidationResult> GetValidationErrors(); Int32 SaveChanges(); Task<Int32> SaveChangesAsync(); Task<Int32> SaveChangesAsync(CancellationToken cancellationToken); DbSet Set(Type entityType); DbSet<TEntity> Set<TEntity>() where TEntity : class; } // IDbContext 

What is the correct way to configure DbContext injection using Autofac?

With StructureMap, I had the following:

 For<IDbContext>().Use(x => new Context()); 
+6
source share
1 answer

Many ways, depending on the required scope, conventions, etc.

Example:

 containerBuilder .RegisterType<Context>() .AsImplementedInterfaces() .InstancePerLifetimeScope(); 
+11
source

Source: https://habr.com/ru/post/984944/


All Articles