Mig*_*ura 9 entity-framework autofac
我有以下EntityFramework上下文:
public class Context : DbContext, IDbContext {
}
Run Code Online (Sandbox Code Playgroud)
其中IDbContext如下:
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
Run Code Online (Sandbox Code Playgroud)
使用Autofac配置DbContext注入的正确方法是什么?
使用StructureMap,我有以下内容:
For<IDbContext>().Use(x => new Context());
Run Code Online (Sandbox Code Playgroud)
Jac*_*goń 14
多种方式,取决于您需要的范围,约定等.
例:
containerBuilder
.RegisterType<Context>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)