【发布时间】:2016-09-18 23:32:30
【问题描述】:
我正在尝试在 .Net Core RC2 中添加脚手架 mvc 视图,但我收到错误 “DbContext RNW.Data.ApplicationDbContext 上没有实体类型 ClientsOverviewViewModel”。
我想通过视图显示客户列表。 我的客户类:
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)
{
}
}
如果我尝试使用客户端而不是视图模型,我也会遇到同样的错误。
到目前为止我尝试过的事情:
- 创建一个不同的 DbContext 类,它派生自 DbContext(这里假设 IdentityDbContext 可能有问题)
- 添加属性
public DbSet<Client> Clients { get; set; } - 添加属性
public DbSet<ClientsOverviewViewModel> Clients { get; set; }(这应该不是必需的,但我想我会尝试一下) - 尝试不同的模板和模型类组合(除了客户端和视图模型),但没有成功
我还没有生成数据库,这可能是个问题吗? 我也在1.0.0-preview1-final
版本中使用entity framework core我也尝试过: 我添加了一个 TempDbContext,它 派生自 DbContext,并且只想添加带有 Model 类 Client 和 Data context 类 TempDbContext 的视图。 然后我得到错误“指定的项目不是列表的元素”
遗憾的是,我找不到任何关于我的问题的博文或 stackoverflow 问题。
【问题讨论】:
-
为什么答案不被接受?
标签: c# entity-framework entity asp.net-core-mvc .net-core-rc2