我正在開發一個.NET MVC 5應用程序與實體框架6. 我已經創建了我的數據庫。然後我做了一個簡單的ViewModel並將其用於HomeController.But當我嘗試映射與控制器數據庫模型視圖模型我得到這個錯誤:使用AutoMapper時激活IConfigurationProvider錯誤
Error activating IConfigurationProvider No matching bindings are available, and the type is not self-bindable. Activation path: 3) Injection of dependency IConfigurationProvider into parameter configurationProvider of constructor of type Mapper 2) Injection of dependency IMapper into parameter mapper of constructor of type HomeController 1) Request for HomeController
Suggestions: 1) Ensure that you have defined a binding for IConfigurationProvider. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.
這是我的HomeController:
public ActionResult Index()
{
var ads = this.adService
.GetAll()
.ToList()
.Select(x => this.mapper.Map<AdViewModel>(x));
var viewModel = new MainViewModel()
{
Ads = ads,
};
return View(viewModel);
//return View();
}
這是我AutoMapperConfiguration:
public class AutoMapperConfig
{
public static IMapperConfigurationExpression Configuration { get; private set; }
public void Execute(Assembly assembly)
{
Mapper.Initialize(
cfg =>
{
var types = assembly.GetExportedTypes();
LoadStandardMappings(types, cfg);
LoadCustomMappings(types, cfg);
Configuration = cfg;
});
}
private static void LoadStandardMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration)
{
var maps = (from t in types
from i in t.GetInterfaces()
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) &&
!t.IsAbstract &&
!t.IsInterface
select new
{
Source = i.GetGenericArguments()[0],
Destination = t
}).ToArray();
foreach (var map in maps)
{
mapperConfiguration.CreateMap(map.Source, map.Destination);
mapperConfiguration.CreateMap(map.Destination, map.Source);
}
}
private static void LoadCustomMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration)
{
var maps = (from t in types
from i in t.GetInterfaces()
where typeof(IHaveCustomMappings).IsAssignableFrom(t) &&
!t.IsAbstract &&
!t.IsInterface
select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray();
foreach (var map in maps)
{
map.CreateMappings(mapperConfiguration);
}
}
}
}
的NinjectConfig:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.BindDefaultInterface();
});
kernel.Bind(x =>
{
x.FromAssemblyContaining(typeof(IService))
.SelectAllClasses()
.BindDefaultInterface();
});
kernel.Bind(typeof(IEfRepository<>)).To(typeof(EfRepository<>));
kernel.Bind(typeof(DbContext), typeof(MsSqlDbContext)).To<MsSqlDbContext>().InRequestScope();
kernel.Bind<ISaveContext>().To<SaveContext>();
kernel.Bind<IMapper>().To<Mapper>();
}
而且在Global.asax:
protected void Application_Start()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MsSqlDbContext, Configuration>());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var mapper = new AutoMapperConfig();
mapper.Execute(Assembly.GetExecutingAssembly());
}
我期待着看到一個想法解決這個錯誤,以及如何注入這個IConfigurationProvider?