0

I work on an ASP.NET Core 7 Web API project with ASP.NET Core Identity.

I wanted to run code-first at the beginning of my project but I skipped because I had no idea how to create foreign key among my models with the AspNetUsers table.

So I chose to run database-first instead.

I migrate the Identity models first, then I create the remaining tables in SMSS. Next I run Scaffold-DbContext to get the models and the new context.

My problem is that when I try to save a new user to the database, I get the error:

System.InvalidOperationException: The entity type 'IdentityUserLogin' requires a primary key to be defined. If you intend to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types

My context looks like this:

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
    {
    }

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

    public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }

    public virtual DbSet<Comment> Comment { get; set; }
    public virtual DbSet<ReactionType> ReactionType { get; set; }
    public virtual DbSet<Video> Video { get; set; }
    public virtual DbSet<VideoReaction> VideoReaction { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AspNetUsers>(entity =>
        {
            entity.HasIndex(e => e.NormalizedUserName, "UserNameIndex")
                .IsUnique()
                .HasFilter("([NormalizedUserName] IS NOT NULL)");
        });

        modelBuilder.Entity<Comment>(entity =>
        {
            entity.Property(e => e.Inserted).HasDefaultValueSql("(getdate())");

            entity.HasOne(d => d.User).WithMany(p => p.Comment)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Comment_AspNetUsers");

            entity.HasOne(d => d.Video).WithMany(p => p.Comment)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Comment_Video");
        });

        modelBuilder.Entity<ReactionType>(entity =>
        {
            entity.Property(e => e.Inserted).HasDefaultValueSql("(getdate())");
        });

        modelBuilder.Entity<Video>(entity =>
        {
            entity.Property(e => e.VideoId).HasDefaultValueSql("(newid())");
            entity.Property(e => e.Inserted).HasDefaultValueSql("(getdate())");

            entity.HasOne(d => d.User).WithMany(p => p.Video)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Video_AspNetUsers");
        });

        modelBuilder.Entity<VideoReaction>(entity =>
        {
            entity.Property(e => e.Inserted).HasDefaultValueSql("(getdate())");

            entity.HasOne(d => d.ReactionType).WithMany(p => p.VideoReaction)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_VideoReaction_ReactionType");

            entity.HasOne(d => d.User).WithMany(p => p.VideoReaction)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_VideoReaction_AspNetUsers");

            entity.HasOne(d => d.Video).WithMany(p => p.VideoReaction)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_VideoReaction_Video");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

When I search for the error message, the only solution I find is that the code below could solve the error. But in my case the code doesn't solve the error.

base.OnModelCreating(builder);

I tried with and still get the error:

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

    modelBuilder.Entity<AspNetUsers>(entity =>
    {
        entity.HasIndex(e => e.NormalizedUserName, "UserNameIndex")
            .IsUnique()
            .HasFilter("([NormalizedUserName] IS NOT NULL)");
    });

    modelBuilder.Entity<Comment>(entity =>
    {
        entity.Property(e => e.Inserted).HasDefaultValueSql("(getdate())");

        entity.HasOne(d => d.User).WithMany(p => p.Comment)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Comment_AspNetUsers");

        entity.HasOne(d => d.Video).WithMany(p => p.Comment)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Comment_Video");
    });
...
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
S. Gunel
  • 13
  • 1
  • 6
  • See [this](https://stackoverflow.com/questions/40703615/the-entity-type-identityuserloginstring-requires-a-primary-key-to-be-defined) – AliK May 21 '23 at 05:57
  • Your error message clearly suggests that you have not assigned a primary key to your entity. Please assign primary key to your entity in `OnModelCreating`: `builder.Entity().HasKey(c => c.YourKey);`. If your error persists, please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Chen May 23 '23 at 03:16

1 Answers1

0

In your table is their any primary key column , if their is no primary key in your table it throws error. Try after adding primary key for your table Or you should add entity.HasNoKey();