在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键"错误? [英] Resolving 'No key Defined' errors while using OnModelCreating with ApplicationDbContext?

查看:26
本文介绍了在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的集合类型创建导航属性,我发现 this example 一个人如何使用 OnModelCreating 完成它.我在我的 MVC 5 应用程序中试了一下,在尝试更新我的数据库时收到了这个错误:

I've been trying to create navigation properties for my collection types and I found this example of how one person accomplished it using OnModelCreating. I gave it a try in my MVC 5 application and I recieved this error when trying to update my database:

在模型生成过程中检测到一个或多个验证错误:

One or more validation errors were detected during model generation:

BlogEngine.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin'没有定义键.定义此 EntityType 的键.BlogEngine.Models.IdentityUserRole: : EntityType 'IdentityUserRole'没有定义键.定义此 EntityType 的键.IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' 是基于没有定义键的类型IdentityUserLogin".IdentityUserRoles:EntityType:EntitySet 'IdentityUserRoles' 是基于在没有定义键的类型IdentityUserRole"上.

BlogEngine.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. BlogEngine.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

如何解决这些未定义键"错误?

我做了一些搜索,发现 此解决方案针对一个用户的问题,但他的需求与我的不同.对于我正在尝试做的事情,这些解决方案似乎相当复杂.

How do I resolve these 'No key Defined' errors?

I did some searching and I found this solution for one users problem, but his needs were different than mine. The solutions seemed pretty convoluted for what I am trying to do.

这是导致问题的代码:

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)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Post>().
                 HasOptional(e => e.Tags).
                 WithMany().
                 HasForeignKey(m => m.Tags_Id);

            modelBuilder.Entity<Tag>().
                 HasOptional(e => e.Posts).
                 WithMany().
                 HasForeignKey(m => m.Posts_Id);
        }

    }

这是我的多对多关系模型:

Here are my to models with the many to many relationship:

Gist 上的 Post.cs 模型

Gist 上的 Tag.cs​​ 模型

更新以显示 IdentityUser:

Update to show IdentityUser:

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

推荐答案

您可能需要在 OnModelCreating

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 });

另见:

这篇关于在使用 OnModelCreating 和 ApplicationDbContext 时解决“未定义键"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆