0

I have an existing application that I am re-writing as .NET Core API with a ReactJS front-end. I am still in the API end, and I've run into a problem.

CODE

I have a BbUser.cs entity class with the following code:

public class BbUser : IdentityUser
{
  public int Points { get; set; } = 0;
  public string DisplayUsername { get; set; }
}

And I also have an Artist.cs entity class:

public class Artist
{
  public int Id { get; set; }
  [Required]
  [MaxLength(50)]
  public string FirstName { get; set; }
  [MaxLength(50)]
  public string LastName { get; set; }
  [MaxLength(100)]
  public string UrlFriendly { get; set; }
  public string ImageUrl { get; set; }
  public bool IsVerified { get; set; }
  public DateTime CreatedOn { get; set; }
  public DateTime ModifiedOn { get; set; }
  public ICollection<Lyric> Lyrics { get; set; } = new List<Lyric>();
  public string UserId { get; set; }
  public BbUser User { get; set; }
}

I need a one-to-many relationship between BbUser and Artist. One user can submit many artists and lyrics ...etc. Simple stuff really.

PROBLEM

The application builds fine, but when I attempt to run it by hitting a controller that requires access to the Database, I get the following error:

The entity type 'IdentityUserLogin' requires a primary key to be defined.

I had this issues with regular EF Code First (not Core) and the fix for that, does not work here.

J86
  • 14,345
  • 47
  • 130
  • 228

2 Answers2

1

This model worked for me(compiled, and no exceptions at runtime) if I used next code in the DbContext class:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<BbUser>(b => b.ToTable("AspNetUsers"));
        base.OnModelCreating(builder);
    }

Without calling base.OnModelCreating(builder) I get the same error, because in this case context isn't applying the Identity related schema.

UPDATE: Everything works fine for me as you can see from the screenshot below: enter image description here

I have one more idea why you can have such an error. Did your BbContext inherit from DbContext class or IdentityDbContext<IdentityUser>? Because I got the same error that was on your screenshot if I used usual DbContext class.

In order to Idenity tables work fine you should use IdentityDbContext<IdentityUser>. Below the whole code for my working DbContext class

 public class BbContext :IdentityDbContext<IdentityUser>
    {
        public BbContext(DbContextOptions options):base(options)
        {
            Database.EnsureCreated();
        }

        public DbSet<Artist> Artists { get; set; }
        public DbSet<Lyric> Lyrics { get; set; }
        public DbSet<Heart> Hearts { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<BbUser>(b => b.ToTable("AspNetUsers"));
            builder.Entity<Heart>().HasKey(h => new {h.UserId, h.LyricId});
            base.OnModelCreating(builder);
        }
    }
user2771704
  • 5,994
  • 6
  • 37
  • 38
  • I just went ahead and [tried what you suggested](https://www.screencast.com/t/xnwgQlAt4). Sadly I get the same error. – J86 Aug 08 '17 at 13:23
  • @Ciwan Strange, can you show the LINQ query which generate such an error? – user2771704 Aug 09 '17 at 06:12
  • I don't have a LINQ query as such. I just want to test to ensure my Db gets created, so I've created a [DummyController](https://gist.github.com/Ciwan1859/e9a2d6ddcee0c2145a2b3e434c4021af) passing in my context via the constructor. This should force the EF Core to create my Database, and as you can see, that error gets in the way of that. – J86 Aug 09 '17 at 08:05
  • @Ciwan I see, please check my updated answer, it worked fine for me using its solution. – user2771704 Aug 09 '17 at 09:22
  • I was using `DbContext`. I changed that for `IdentityDbContext` and it all now works fine. Thank you v. much. – J86 Aug 09 '17 at 09:43
0

Please do try declaring a Guid property named Id, with both Get and Set on the IdentityUserLogin entity.

Another option is to declare a property and decorate it with [Key] attribute

Leonardo
  • 10,737
  • 10
  • 62
  • 155