c# - What could be reason of AutoMapper failed? -
i suppose reason of automapper failed different fields of domain model , view model. here domain model:
public partial class users { public int id { get; set; } public string login { get; set; } public string password { get; set; } public int roleid { get; set; } public virtual userroles userroles { get; set; } }
so here view model:
public class usersviewmodel { public int id { get; set; } [required] [minlength(3, errormessage = "Минимальная длина логина - 3 символа")] [maxlength(50, errormessage = "Максимальная длина логина - 50 символов")] [display(name = "Логин")] public string login { get; set; } [required] [minlength(8, errormessage = "Минимальная длина пароля - 8 символов")] [maxlength(50, errormessage = "Максимальная длина пароля - 50 символов")] [display(name = "Пароль")] public string password { get; set; } [required] [display(name = "Роль")] public int roleid { get; set; } public ienumerable<selectlistitem> usrroles { get; set; } public virtual userroles userroles { get; set; } }
is filed public ienumerable<selectlistitem> usrroles { get; set; }
reason why automapper failed?
i call mapping way:
public async task<ienumerable<usersviewmodel>> getuserslist() { var t = new task<ienumerable<users>>(() => _db.users.toenumerable()); t.start(); await t; var vm = mapper.map<task<ienumerable<users>>, task<ienumerable<usersviewmodel>>>(t); return vm.result.orderby(x => x.login); }
it's failing because trying map task
's not users
usersviewmodel
. want:
public async task<ienumerable<usersviewmodel>> getuserslist() { var users = await new task<ienumerable<users>>(() => _db.users.toenumerable()); var vm = mapper.map<ienumerable<users>, ienumerable<usersviewmodel>>(users); return vm.result.orderby(x => x.login); }
await
return result of task, want, not task itself.
your other option calling t.result
return ienumerable<users>
list.
Comments
Post a Comment