我在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; }
}
Run Code Online (Sandbox Code Playgroud)
在数据库上下文中,我有以下代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
我希望微软以IdentityUser这种方式实现对象,它可以在其他实体类型中使用,所以我可能做错了,因为当我尝试为Content实体类型创建一个控制器时,我得到以下内容错误:
EntityType 'IdentityUserLogin' has no key defined
EntityType 'IdentityUserRole' has no key defined
EntityType: EntitySet 'IdentityUserLogins' is …Run Code Online (Sandbox Code Playgroud) 我一直想为我的集合类型创建导航属性,我发现这个例子中的一个人如何完成它使用OnModelCreating.我在我的MVC 5应用程序中试了一下,在尝试更新我的数据库时收到了这个错误:
在模型生成期间检测到一个或多个验证错误:
BlogEngine.Models.IdentityUserLogin :: EntityType'IdentityUserLogin'没有定义键.定义此EntityType的键.BlogEngine.Models.IdentityUserRole :: EntityType'IdentityUserRole'没有定义键.定义此EntityType的键.IdentityUserLogins:EntityType:EntitySet'IdentityUserLogins'基于没有定义键的类型'IdentityUserLogin'.IdentityUserRoles:EntityType:EntitySet'IdentityUserRoles'基于类型'IdentityUserRole',没有定义键.
我做了一些搜索,我找到了解决一个用户问题的解决方案,但他的需求与我的不同.对于我想要做的事情,解决方案似乎很复杂.
这是导致问题的代码:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
//public DbSet<Image> Images { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Post> Posts { get; set; }
public DbSet<Image> Images { get; set; }
public DbSet<Album> Albums { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) …Run Code Online (Sandbox Code Playgroud)