为什么我不能使用AutoMapper将`ApplicationUser`映射到视图模型?

时间:2015-09-03 21:36:42

标签: asp.net-mvc entity-framework asp.net-identity automapper

我的ApplicationUser是相当标准的,由MVC 5项目模板生成,添加了FullNameIPayCaddyEntity界面:

public class ApplicationUser : IdentityUser, IPayCaddyEntity
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public string FullName { get; set; }
}

新界面仅限:

public interface IPayCaddyEntity
{
    string Id { get; set; }
}

已由具有IdentityUser string属性的Id实施。一切都建立,大多数运行正常。然后我想将用户列表映射到viewmodel:

public class PlayerViewModel: PayCaddyViewModel
{
    public string FullName { get; set; }
}

它们应该很好地映射,两者都具有相同的类型FullName属性。我试试这个:

Mapper.CreateMap<ApplicationUser, PlayerViewModel>().ReverseMap();
Mapper.AssertConfigurationIsValid();

然后这个:

var players = _db.Users.ToList()
var model = Mapper.Map<IList<PlayerViewModel>>(players);

Map调用会引发异常。

我正在尝试将Identity实体包含在我自己的数据库中,而Users属性是DbSet<ApplicationUser,我的DbContext继承自IdentityDbContext<ApplicationUser>

完整堆栈跟踪是:

[AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
ApplicationUser -> PlayerViewModel
PayCaddy.Data.Models.ApplicationUser -> PayCaddy.Client.Models.PlayerViewModel

Destination path:
IList`1[0]

Source value:
System.Data.Entity.DynamicProxies.ApplicationUser_4342C7C6E2802320D156341C80F8DED74454F28D43628C766C6951DB971B9BDA]
   AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +610
   AutoMapper.Mappers.EnumerableMapperBase`1.Map(ResolutionContext context, IMappingEngineRunner mapper) +482
   AutoMapper.Mappers.CollectionMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +126
   AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +610
   AutoMapper.MappingEngine.MapCore(Object source, Type sourceType, Type destinationType, MappingOperationOptions options) +179
   AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts) +59
   AutoMapper.MappingEngine.Map(Object source, Action`1 opts) +92
   AutoMapper.MappingEngine.Map(Object source) +93
   AutoMapper.Mapper.Map(Object source) +62
   PayCaddy.Client.Controllers.<PlayersIndex>d__11.MoveNext() in C:\Development\Cordova\PayCaddy\PayCaddy.Client\Controllers\AccountController.cs:65
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9723757
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 个答案:

答案 0 :(得分:1)

您的问题必须是parseInt("10", 0); parseInt("10", 1); parseInt("10", 2); parseInt("10", 3); 未被调用,因为您的代码可以正常工作,就像您在this .NET fiddle中看到的那样。

仔细检查是否正确调用了["10", "10", "10", "10"].map(function (value) { return parseInt(value); }); //[10, 10, 10, 10] 。尝试在Application_Start()方法,Global.asax.cs文件中调用它。