protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); Database.SetInitializer <MusicstoreContext>(null); Database.SetInitializer(new CreateDatabaseIfNotExists <MusicstoreContext>()); Database.SetInitializer(new MigrateDatabaseToLatestVersion <MusicstoreContext, Data.Migrations.Configuration>()); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; var builder = new ContainerBuilder(); #region data layer //var dataSettingsManager = new DataSettingsManager(); //var dataProviderSettings = dataSettingsManager.LoadSettings(); #endregion var db = new MusicstoreContext(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //builder.RegisterGeneric(typeof(EfRepository<>)).AsImplementedInterfaces(); builder.Register <IRepository>(c => new EfRepository(db)).InstancePerApiRequest(); //builder.Register<IRepository<Artist>>(c => new EfRepository<Artist>(new MusicstoreContext())).InstancePerApiRequest(); //builder.Register<IRepository<Song>>(c => new EfRepository<Song>(new MusicstoreContext())).InstancePerApiRequest(); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = resolver; }
/// <summary> /// Ctor /// </summary> /// <param name="context">Object context</param> public EfRepository(MusicstoreContext context) { _context = context; ////SERIALIZE WILL FAIL WITH PROXIED ENTITIES //_context.Configuration.ProxyCreationEnabled = false; ////ENABLING COULD CAUSE ENDLESS LOOPS AND PERFORMANCE PROBLEMS //_context.Configuration.LazyLoadingEnabled = false; }