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