【发布时间】:2014-08-18 11:22:19
【问题描述】:
所以我想做的是使用我的 DBContext 从数据库中获取一些信息以进行映射。
所以我创建了一个自定义的 TypeConverter:
public class RoundVMtoTrampetRound : ITypeConverter<RoundVM, TrampetRound>
{
public RoundVMtoTrampetRound(DBTariff context)
{
this.context = context;
}
private DBTariff context { get; set; }
public TrampetRound Convert(ResolutionContext context)
{
RoundVM source = (RoundVM)context.SourceValue;
Mapper.CreateMap<RoundVM, TrampetRound>();
var dest = Mapper.Map<TrampetRound>(source);
dest.Difficulty = this.context.DifficultyTrampet.Find(source.Id);
return dest;
}
}
在我的控制器中,我使用以下方法创建了一个映射器:
Mapper.CreateMap<RoundVM, TrampetRound>().ConvertUsing<RoundVMtoTrampetRound>();
但是当我进行映射时,我收到错误消息说没有默认构造函数。但我希望 ninject 使用代码为我执行此操作:
kernel.Bind<DBTariff>().ToSelf().InRequestScope();
答案在这里,但我仍然遇到同样的问题 Automapper + EF4 + ASP.NET MVC - getting 'context disposed' error (I know why, but how to fix it?)
我已经尝试了链接中给出的解决方案,但得到了同样的错误。 那么如何让 Automapper 使用我的 Ninject 来解决问题呢?
编辑
我还发现这个用 autofac 完成了同样的事情 http://thoai-nguyen.blogspot.se/2011/10/autofac-automapper-custom-converter-di.html 所以我的猜测是我需要告诉 automapper 使用我的 ninject 解析器,但我该怎么做以及在哪里做?
【问题讨论】:
标签: c# asp.net-mvc entity-framework ninject automapper