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

4
我正在尝试在.Net Core RC2中添加一个脚手架MVC视图,但是我遇到了错误"There is no entity type ClientsOverviewViewModel on DbContext RNW.Data.ApplicationDbContext"

Add View to project

我想用这个视图来显示客户列表。我的客户类:

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,则会出现同样的错误。
到目前为止我尝试过以下几点:
1. 创建一个不同的DbContext类,它派生自DbContext(这里的假设是IdentityDbContext可能存在问题) 2. 添加一个属性public DbSet Clients { get; set; } 3. 添加一个属性public DbSet Clients { get; set; }(这不应该是必要的,但我想尝试一下) 4. 尝试不同模板和模型类的组合(除了Client和视图模型之外),但没有成功。
我还没有生成数据库,这可能是问题吗? 我也在版本为1.0.0-preview1-final的entity framework core中使用。
我也尝试过以下内容: 我添加了一个TempDbContext,它派生自DbContext,并只想用Model类Client和Data context类TempDbContext添加视图。 然后我得到了错误“The item specified is not the element of a list”。
遗憾的是我无法找到任何关于我的问题的博客文章或stackoverflow问题。

为什么答案没有被接受? - Sergi0
2个回答

3

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


这在我的情况下是一个问题,我在 Id 字段名称中有一个拼写错误。 - Sergi0
2
我遇到了类似的问题,我的ViewModel中有一个public Uri Url { get; set; }。显然,代码生成器不喜欢在那里使用Uri类型。在Visual Studio中,“代码生成日志”输出在我阅读了你的答案后指向了大致方向,即ViewModel类可能存在问题。 - Wimpje

0
我遇到的问题是EF不支持泛型列表(List)。一旦我改变了这个,我就能够使用脚手架工具。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接