我是ASP.net MVC的新手。我正在尝试创建一个视图模型来显示数据连接。下面是一些示例代码:如何正确使用视图模型
public class Person
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public ICollection<Relative> Relatives { get; set; }
}
public class Relative
{
[Key]
public int ID {get; set; }
public Person Person { get; set; }
public RelationType RelationType { get; set; }
}
public class RelationType
{
[Key]
public int ID { get; set; }
public string Description { get; set; }
}
public class PersonViewModel
{
public string Name { get; set; }
public ICollection<string> RelativeNames { get; set; }
public ICollection<string> RelativeTypes { get; set; }
}
public class PersonContext : DbContext
{
public DbSet<PersonViewModel> people { get; set; }
}
当我尝试创建通过Visual Studio我的控制,我得到以下错误:
无法检索元数据PersonViewModel。在生成期间检测到一个或多个验证错误: EntityType'PersonViewModel'没有定义密钥。定义此EntityType的关键字。
啊。我只是困惑。我从来没有用主键指定视图;我在考虑SQL Server视图。 – 2012-08-13 00:42:41
哦,ViewModel绝对不是Domain Model。看到[做viewmodels需要键?](http://stackoverflow.com/questions/20816115/do-viewmodels-need-keys) – Aigori 2014-08-21 15:22:04