C# 实体类型 MVC5 EF6 中的用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19913447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User in Entity type MVC5 EF6
提问by Gunnar
I have created a class in MVC5, where I want a primary owner of the content and then I want to have some editors for the content:
我在 MVC5 中创建了一个类,我想要内容的主要所有者,然后我想要一些内容编辑器:
public class Content
{
public int ID { get; set; }
public IdentityUser Owner { get; set; }
public ICollection<IdentityUser> Editors { get; set; }
public string Title{ get; set; }
public string Body { get; set; }
}
In the database context I have the following code:
在数据库上下文中,我有以下代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Content>()
.HasOptional(c => c.Editors)
.WithRequired()
.WillCascadeOnDelete();
modelBuilder.Entity<Content>()
.HasRequired(c => c.Owner)
.WithOptional()
.WillCascadeOnDelete();
}
I would expect Microsoft to have implemented the IdentityUser
object in such a way, that it can be used in other entity types, so I am probably doing something wrong, because when I try to make a controller for the Content entity type, I get the following error:
我希望 Microsoft 以IdentityUser
这种方式实现该对象,以便它可以在其他实体类型中使用,所以我可能做错了什么,因为当我尝试为 Content 实体类型创建控制器时,我得到以下信息错误:
EntityType 'IdentityUserLogin' has no key defined
EntityType 'IdentityUserRole' has no key defined
EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no key defined
EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no key defined
I have also tried to add the following code to the ApplicationDbContext
as described in this question Map tables using fluent api in asp.net MVC5 EF6?:
我还尝试将以下代码添加到ApplicationDbContext
这个问题Map tables using fluent api in asp.net MVC5 EF6? :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
I must be doing something wrong. Please tell me how I can handle users in my Content EntityType.
我一定做错了什么。请告诉我如何处理我的 Content EntityType 中的用户。
采纳答案by Olav Nyb?
The following method, OnModelCreating
, will create a working context. Not sure if it is the mapping you want though.
以下方法OnModelCreating
将创建一个工作上下文。不确定它是否是您想要的映射。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Content>()
.HasMany(c => c.Editors)
.WithOptional()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Content>()
.HasRequired(c => c.Owner)
.WithOptional()
.WillCascadeOnDelete(false);
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
回答by Aaron
I had the same problem and I found that I had not called
我有同样的问题,我发现我没有打电话
base.OnModelCreating(modelBuilder);
in my DbModel
在我的 DbModel 中