首页 文章

无法在.Net Core RC2 中添加视图

提问于
浏览
3

我试图在.Net Core RC2 中添加一个脚手架 mvc 视图,但我得到错误**“DbContext RNW.Data.ApplicationDbContext 上没有实体类型 ClientsOverviewViewModel”**。

将 View 添加到项目中

在视图中我想显示一个 Client 列表。我的客户类:

public class Client : Person
{
    #region Personal Data 
    public Nationality Nationality { get; set; }
    public Confession Confession { get; set; }
    public string SSN { get; set; }
    public MaritalStatus MaritalStatus { get; set; }
    #endregion
    ...
}

    public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public Sex Sex { get; set; }
    public DateTime Birthday { get; set; }
    public Address Birthplace { get; set; }
    public Address ResidentialAddress { get; set; }
    public string EMail { get; set; }
    public string PhoneNumber { get; set; }
}

在列表中,我想显示 5 个属性,我已将其放入 ViewModel:

public class ClientsOverviewViewModel
{
    [Display(Name = "Nachname")]
    public string LastName { get; set; }
    [Display(Name = "Vorname")]
    public string FirstName { get; set; }
    [Display(Name = "Geschlecht")]
    public Sex Sex { get; set; }
    [Display(Name = "Staatsbürgerschaft")]
    public string Nationality { get; set; }
    [Display(Name="Geburtsdatum")]
    public DateTime? DateOfBirth { get; set; }
}

这里还有我的 ApplicationDbContext 类:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

如果我尝试使用 Client 而不是 viewmodel,我也会得到同样的错误。

到目前为止我尝试过的事情:

  • 创建一个不同的 DbContext 类,它派生自 DbContext(假设这里是 IdentityDbContext 可能有问题)

  • 添加属性public DbSet<Client> Clients { get; set; }

  • 添加一个属性public DbSet<ClientsOverviewViewModel> Clients { get; set; }(这不应该是必要的,但我想我会试一试)

  • 尝试模板和模型类的不同组合(除了 Client 和 viewmodel),但没有成功

我还没有生成数据库,这可能是个问题吗?我也在版 1.0.0-preview1-final中使用实体框架核心

我也尝试过:我添加了一个TempDbContext,它来自 DbContext,只是想用 Model 类Client 和 Data context classTempDbContext 添加视图.然后我得到错误“指定的项目不是列表的元素”**

可悲的是,我无法找到有关我的问题的任何博客文章或 stackoverflow 问题。

2 回答

  • 3

    您的 ViewModel 类必须有一个键。名为Id的属性可以正常工作。

  • 0

    我遇到的问题是 EF 不支持通用列表(List)。一旦我改变了这一点,我就可以使用脚手架工具了。

相关问题