【发布时间】:2017-10-06 22:14:01
【问题描述】:
我正在尝试从数据库中获取我的数据以显示在我的视图中。目前在我的数据库表中有这些列:ID、ReportDescription、DateReported、CustomerId 和 MovieId。
我需要做的是使用 CustomerId 和 MovieId 来获取客户的名称和电影的名称。
这是我的模特:
public class Malfunction
{
public int Id { get; set; }
[Required]
public Movie Movie { get; set; }
[Required]
public Customer Customer { get; set; }
[Required]
[StringLength(255)]
public string ReportDescription { get; set; }
public DateTime DateReported { get; set; }
}
这是控制器:
private ApplicationDbContext _context;
public TablesController()
{
_context = new ApplicationDbContext();
}
// GET: Tables
public ActionResult MalfunctionsList(MalfunctionDTO malfunctionDTO)
{
var malfunctions = _context.Malfunctions.ToList();
return View(malfunctions);
}
}
这是 DTO:
public class MalfunctionDTO
{
public int CustomerId { get; set; }
public List<int> MovieIds { get; set; }
public Customer Customer { get; set; }
public Movie Movie { get; set; }
public string ReportDescription { get; set; }
}
身份模型:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<MembershipType> MembershipTypes { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Rent> Rents { get; set; }
public DbSet<Malfunction> Malfunctions { get; set; }
我是否应该在我的模型中添加诸如 CustomerId 和 MovieId 之类的属性以使其正常工作? 值得注意的是,我对 MVC 和 EF 很陌生,所以如果我有点困惑,请原谅我。
【问题讨论】:
-
您是否在 DTO 和 ViewModel 之间配置了映射?
-
@Thennarasan 我在我的 MappingProfile Mapper.CreateMap
();.. 即使我删除了这个地图,代码的工作方式也是一样的。另外值得注意的是,我没有为此使用 viewModel,我应该这样做吗? -
将此
var malfunctions = _context.Malfunctions.ToList();更改为var malfunctions = Mapper.Map<List<Malfunction>>(_context.Malfunctions.ToList());` 希望在您看来您正在调用Malfunction模型 -
如果您在模型和 Dto 中使用不同的名称,那么您需要在 AutoMapper CreateMap 部分中映射它们。
-
@Thennarasan AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射
标签: c# asp.net-mvc entity-framework