title: Autofac Dependency Injection - Data Not Saving to Database
body: <p>I am trying to implement Dependency Injection with Autofac. I'm trying to add a WebAPI interface to an MVC application. My goal is to create an application that communicates via API while creating an administration panel. I don't get any error message but data is not saved in database. I think it has something to do with the <code>Register&lt;EFUnitOfWork&gt;</code> or the <code>Register&lt;DbContext&gt;</code> field.</p>
  <p>Business Layer -&gt; AutofacBusinessModule</p>
  <pre><code> public class AutofacBusinessModule : Module
      {
          protected override void Load(ContainerBuilder builder)
          {
              builder.RegisterType&lt;ArticleManager&gt;().As&lt;IArticleService&gt;();
              builder.RegisterType&lt;EfArticleDal&gt;().As&lt;IArticleDal&gt;();
              
              builder.RegisterType&lt;CategoryManager&gt;().As&lt;ICategoryService&gt;();
              builder.RegisterType&lt;EfCategoryDal&gt;().As&lt;ICategoryDal&gt;();
              
              builder.RegisterType&lt;CommentManager&gt;().As&lt;ICommentService&gt;();
              builder.RegisterType&lt;EfCommentDal&gt;().As&lt;ICommentDal&gt;();
              
              builder.RegisterType&lt;RoleManager&gt;().As&lt;IRoleService&gt;();
              builder.RegisterType&lt;EfRoleDal&gt;().As&lt;IRoleDal&gt;();
              
              builder.RegisterType&lt;UserManager&gt;().As&lt;IUserService&gt;();
              builder.RegisterType&lt;EfUserDal&gt;().As&lt;IUserDal&gt;();
              
              builder.RegisterType&lt;ATKlogMSSqlContext&gt;().InstancePerLifetimeScope();
              builder.RegisterType&lt;EfUnitOfWork&gt;().As&lt;IUnitOfWork&gt;().AsSelf().SingleInstance();
              
              var assembly = System.Reflection.Assembly.GetExecutingAssembly();
              builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces()
                  .EnableInterfaceInterceptors(new ProxyGenerationOptions()
                  {
                      Selector = new AspectInterceptorSelector()
                  }).SingleInstance();
  
          }
      }
  </code></pre>
  <p>Business Layer -&gt; Article Manager</p>
  <pre><code>public class ArticleManager : IArticleService
      {
          private readonly IUnitOfWork _unitOfWork;
          private readonly IMapper _mapper;
  
          public ArticleManager(IUnitOfWork unitOfWork, IMapper mapper)
          {
              _unitOfWork = unitOfWork;
              _mapper = mapper;
          }
  
          [ValidationAspect(typeof(ArticleAddDtoValidator))]
          public async Task&lt;IResult&gt; Add(ArticleAddDto articleAddDto, string createdByName)
          {
              var mapping = _mapper.Map&lt;Article&gt;(articleAddDto);
              mapping.CreatedByName = createdByName;
              mapping.CreatedDate = DateTime.Now;
              mapping.ModifiedByName = createdByName;
              mapping.ModifiedDate = DateTime.Now;
              mapping.Slug = SlugifyHelper.ConvertSlug(articleAddDto.Title);
              mapping.UserId = 1;
  
              await _unitOfWork.Articles.AddAsync(mapping);
              await _unitOfWork.SaveAsync();
              return new SuccessResult(String.Format(Messages.Article.ArticleAdded, articleAddDto.Title));
          }
  
      }
  </code></pre>
  <p>DataAccess Layer -&gt; EFUnitOfWork</p>
  <pre><code>public class EfUnitOfWork : IUnitOfWork
      {
          private readonly ATKlogMSSqlContext _context;
          private EfArticleDal _efArticleDal;
          private EfCategoryDal _efCategoryDal;
          private EfCommentDal _efCommentDal;
          private EfRoleDal _efRoleDal;
          private EfUserDal _efUserDal;
  
          public EfUnitOfWork(ATKlogMSSqlContext context)
          {
              _context = context;
          }
  
          public async ValueTask DisposeAsync()
          {
              await _context.DisposeAsync();
          }
  
          public IArticleDal Articles =&gt; _efArticleDal ?? new EfArticleDal();
          public ICategoryDal Categories =&gt; _efCategoryDal ?? new EfCategoryDal();
          public ICommentDal Comments =&gt; _efCommentDal ?? new EfCommentDal();
          public IRoleDal Roles =&gt; _efRoleDal ?? new EfRoleDal();
          public IUserDal Users =&gt; _efUserDal ?? new EfUserDal();
  
          public async Task&lt;int&gt; SaveAsync()
          {
              return await _context.SaveChangesAsync();
          }
      }
  </code></pre>
  <p>MVC -&gt; Startup</p>
  <pre><code>public class Startup
      {
          // This method gets called by the runtime. Use this method to add services to the container.
          // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddControllersWithViews().AddRazorRuntimeCompilation().AddNewtonsoftJson(opt =&gt; opt.SerializerSettings.ReferenceLoopHandling =
                  Newtonsoft.Json.ReferenceLoopHandling.Ignore);
              services.AddAutoMapper(typeof(ArticleProfile), typeof(CategoryProfile), typeof(CommentProfile), typeof(UserProfile));
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
                  app.UseStatusCodePages();
              }
  
              app.UseStaticFiles();
  
              app.UseRouting();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapAreaControllerRoute(
                      name: &quot;Admin&quot;,
                      areaName: &quot;Admin&quot;,
                      pattern: &quot;Admin/{controller=Home}/{action=Index}/{id?}&quot;
                  );
                  endpoints.MapDefaultControllerRoute();
                  endpoints.MapControllers();
              });
          }
      }
  </code></pre>
  <p>MVC Layer -&gt; Program</p>
  <pre><code>public class Program
      {
          public static void Main(string[] args)
          {
              CreateHostBuilder(args).Build().Run();
          }
  
          public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                  .ConfigureContainer&lt;ContainerBuilder&gt;(builder =&gt;
                  {
                      builder.RegisterModule(new AutofacBusinessModule());
                  })
                  .ConfigureWebHostDefaults(webBuilder =&gt; { webBuilder.UseStartup&lt;Startup&gt;(); });
      }
  </code></pre>