【问题标题】:Show data from database table显示数据库表中的数据
【发布时间】: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&lt;List&lt;Malfunction&gt;&gt;(_context.Malfunctions.ToList()); ` 希望在您看来您正在调用Malfunction 模型
  • 如果您在模型和 Dto 中使用不同的名称,那么您需要在 AutoMapper CreateMap 部分中映射它们。
  • @Thennarasan AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射

标签: c# asp.net-mvc entity-framework


【解决方案1】:

您需要将映射更改为如下所示。

// 为Id

Mapper.CreateMap<Malfunction, MalfunctionDTO>()
                .ForMember(dest => dest.CustomerId, 
                     opt => opt.MapFrom(src => src.Id));

更新

我假设您没有使用 Entity Framework Core。

如下所示更新您的上下文。 vrtual 添加了关键字。

public virtual DbSet<Movie> Movies { get; set; }
public virtual DbSet<Customer> Customers { get; set; }

也要改

public class Malfunction
{
    public int Id { get; set; }

    [Required]
    public virtual Movie Movie { get; set; }

    [Required]
    public virtual Customer Customer { get; set; }

    [Required]
    [StringLength(255)]
    public string ReportDescription { get; set; }

    public DateTime DateReported { get; set; }
}

那么您可能需要在OnModelCreatingmethod 中添加类似这样的配置。先尝试不使用此功能,如果不起作用,请添加以下配置。

modelBuilder.Entity() .WithRequired(m => m.Movie);

modelBuilder.Entity() .WithRequired(m => m.Customer);

【讨论】:

  • 客户值仍为空,不显示在视图上。
  • 好的,你从数据库中得到了什么?我的意思是在映射到 Dto 之前?
  • 即使我的 MappingProfile 中没有地图,我也会得到故障.Id、故障.ReportDescription 和故障.DateReported。我没有得到故障。客户和故障。电影,因为当它们到达视图时它们似乎为空
  • 您需要启用延迟加载才能获取相关实体。您使用什么版本的 EntityFramework?在您的问题中提供您的 ApplicationDbContext 类和电影类是值得的。
  • 在帖子中添加了 ApplicationDbContext,EF 版本 6.0.0.0
猜你喜欢
  • 2019-04-08
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
相关资源
最近更新 更多