TransWikia.com

'Aspnetuserlogins' requires a primary key to be defined (interconnected databases)

Stack Overflow Asked by Bubinga on November 25, 2020

I have scaffolded a DBContext from my database that only contains Identity tables. It’s one of two DBContexts, the other coming from another database which contains foreign keys to certain columns in the Identity Database. However I am getting a runtime error of:

The entity type ‘Aspnetuserlogins’ requires a primary key to be defined. If you intended to use a keyless entity type call ‘HasNoKey()’

I have the base.OnModelCreating(modelBuilder); line, so I have no idea what is causing this issue.
Here is my context:

public partial class UserContext : IdentityDbContext
    {
        public UserContext()
        {
        }

        public UserContext(DbContextOptions<UserContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Aspnetroleclaims> Aspnetroleclaims { get; set; }
        public virtual DbSet<Aspnetroles> Aspnetroles { get; set; }
        public virtual DbSet<Aspnetuserclaims> Aspnetuserclaims { get; set; }
        public virtual DbSet<Aspnetuserlogins> Aspnetuserlogins { get; set; }
        public virtual DbSet<Aspnetuserroles> Aspnetuserroles { get; set; }
        public virtual DbSet<Aspnetusers> Aspnetusers { get; set; }
        public virtual DbSet<Aspnetusertokens> Aspnetusertokens { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseMySql("UserDB", x => x.ServerVersion("8.0.20-mysql"));
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Aspnetroleclaims>(entity => ...});
            modelBuilder.Entity<Aspnetroles>(entity => ...});
            modelBuilder.Entity<Aspnetuserclaims>(entity => ...});
            modelBuilder.Entity<Aspnetuserlogins>(entity =>
            {
                entity.HasKey(e => new { e.LoginProvider, e.ProviderKey })
                    .HasName("PRIMARY");

                entity.HasIndex(e => e.UserId)
                    .HasName("IX_AspNetUserLogins_UserId");

                entity.Property(e => e.LoginProvider)
                    .HasCharSet("utf8mb4")
                    .HasCollation("utf8mb4_0900_ai_ci");

                entity.Property(e => e.ProviderKey)
                    .HasCharSet("utf8mb4")
                    .HasCollation("utf8mb4_0900_ai_ci");

                entity.Property(e => e.ProviderDisplayName)
                    .HasCharSet("utf8mb4")
                    .HasCollation("utf8mb4_0900_ai_ci");

                entity.Property(e => e.UserId)
                    .HasCharSet("utf8mb4")
                    .HasCollation("utf8mb4_0900_ai_ci");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Aspnetuserlogins)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK_AspNetUserLogins_AspNetUsers_UserId");
            });
            modelBuilder.Entity<Aspnetuserroles>(entity => ...});
            modelBuilder.Entity<Aspnetusers>(entity => ...});
            modelBuilder.Entity<Aspnetusertokens>(entity => ...});

            OnModelCreatingPartial(modelBuilder);          
        }
        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }

And here is the aspnetuserlogins model:

[Table("aspnetuserlogins")]
    public partial class Aspnetuserlogins
    {
        //I tried putting a [Key] here, but got a "use Fluent API error"
        [Column(TypeName = "varchar(255)")]
        public string LoginProvider { get; set; }
        //I tried putting a [Key] here, but got a "use Fluent API error"
        [Column(TypeName = "varchar(255)")]
        public string ProviderKey { get; set; }
        [Column(TypeName = "longtext")]
        public string ProviderDisplayName { get; set; }
        [Required]
        [Column(TypeName = "varchar(255)")]
        public string UserId { get; set; }

        [ForeignKey(nameof(UserId))]
        [InverseProperty(nameof(Aspnetusers.Aspnetuserlogins))]
        public virtual Aspnetusers User { get; set; }
    }

Edit: I tried putting in some breakpoints and it’s failing on this line:

 IList<Timelineinfo> SimilarEvents = _datacontext.Timelineinfo
                                     .Where(t => t.Date.Equals(TempDate))
                                     .ToList();

_datacontext is the other DbContext, TempDate is a date as a string, and Timelineinfo looks like:

 [Key]
 public int IdTimelineinfo { get; set; }
 [Required]       
 [MaxLength(10)]
 [DataType(DataType.Date)]
 [Column(TypeName = "char(10)")]
 public string Date { get; set; }
 public byte State { get; set; }
 Required]
 [Column(TypeName = "varchar(20)")]
 public string City { get; set; }
 public byte Misconduct { get; set; }
 public byte? Weapon { get; set; }
 [Required]
 [Column(TypeName = "mediumtext")]
 public string Context { get; set; }
 public byte Locked { get; set; }
 [Column(TypeName = "varchar(255)")]
 public string SubmittedBy { get; set; }
 public byte Verified { get; set; }
 [ForeignKey(nameof(SubmittedBy))]
 [InverseProperty(nameof(Aspnetusers.Timelineinfo))]
 public virtual Aspnetusers SubmittedByNavigation { get; set; }

This question is similar to this one but there are no answers there that worked

One Answer

The issue is because I was attempting to load from two interconnected databases without letting EF Core know that the data one is dependent on the user one. The problem can be fixed by changing the code to the following:

public partial class DataContext : UserContext {..}
public partial class UserContext : DbContext
    {
        public UserContext()
        {
        }

        public UserContext(DbContextOptions options)
            : base(options)
        {
        }
}

Correct answer by Bubinga on November 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP