在MVC 5应用程序中AutoMapper可能存在的问题

时间:2017-03-15 11:33:03

标签: asp.net-mvc-5 entity-framework-6 automapper

我正在尝试使用Entity Framework数据库第一种方法在ASP.NET MVC 5应用程序中使用存储过程来获取存储过程的结果集。

我已将automapper设置为使用我创建的实体映射结果对象(复杂类型)。控制器返回带有空值的结果集。我使用AutoMapper来映射这两个对象。我可以看到AutoMapper配置在获取记录之前执行但不明白为什么它不返回值。我检查了数据库中的存储过程,它确实返回了记录。请找到以下代码。

浏览器输出

{"Data":[{"TeamID":0,"CountryCode":null,"TeamName":null,"TeamDescription":null,"IsActive":0,"CreatedDate":"\/Date(-62135596800000)\/","CreatedBy":null,"ModifiedDate":"\/Date(-62135596800000)\/","ModifiedBy":null},{"TeamID":0,"CountryCode":null,"TeamName":null,"TeamDescription":null,"IsActive":0,"CreatedDate":"\/Date(-62135596800000)\/","CreatedBy":null,"ModifiedDate":"\/Date(-62135596800000)\/","ModifiedBy":null},{"TeamID":0,"CountryCode":null,"TeamName":null,"TeamDescription":null,"IsActive":0,"CreatedDate":"\/Date(-62135596800000)\/","CreatedBy":null,"ModifiedDate":"\/Date(-62135596800000)\/","ModifiedBy":null},{"TeamID":0,"CountryCode":null,"TeamName":null,"TeamDescription":null,"IsActive":0,"CreatedDate":"\/Date(-62135596800000)\/","CreatedBy":null,"ModifiedDate":"\/Date(-62135596800000)\/","ModifiedBy":null}],"Total":4,"AggregateResults":null,"Errors":null}

enter image description here

项目中定义的实体

public abstract class BaseEntity
{
        public DateTimeOffset CreatedDate { get; set; }
        public string CreatedBy { get; set; }
        public DateTimeOffset ModifiedDate { get; set; }
        public string ModifiedBy { get; set; }
}

public class Team : BaseEntity
{
        public int TeamID { get; set; }
        public string CountryCode { get; set; }
        public string  TeamName { get; set; }
        public string TeamDescription { get; set; }
        public int IsActive { get; set; }
}

控制器

public ActionResult Team([DataSourceRequest]DataSourceRequest request)
{
    return Json(mcrRepository.GetTeams().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}

存储库

public class McrRepository
{
        MCREntities db = new MCREntities();

        public List<Team> GetTeams()
        {
            if (ADHelper.UserValidate() == 1)
            {
                var teams = db.spGetAllTeams("");
                return teams.Select(t => new Team()).ToList();
            }
            else
            {
                return null;
            }
        }
}

映射

public class AutoMapperConfiguration
{
        public static void Configure()
        {
            Assembly[] assemblies = BuildManager.GetReferencedAssemblies().OfType<Assembly>().ToArray();
            Mapper.Initialize(cfg =>
            cfg.AddProfiles(AllClasses.FromAssemblies(assemblies)
            .Where(
            a =>
               a.FullName.EndsWith("Mapping"))));
        }
}

public class DomainToModelMapping : Profile
{
        public DomainToModelMapping()
        {
             CreateMap<spGetAllTeams_Result, Team>().ReverseMap(); 
        }
}

0 个答案:

没有答案