带有EF Core Code First和IdentityUser的.Net Core API [英] .Net Core API with EF Core Code First and IdentityUser
问题描述
我有一个
我还有一个主意,为什么会出现这样的错误.您的BbContext是从 DbContext
类还是 IdentityDbContext< IdentityUser>
继承的?因为如果我使用通常的 DbContext
类,则会遇到与您的屏幕快照相同的错误.
为了使Idenity表正常工作,您应该使用 IdentityDbContext< IdentityUser>
.下面是我正在工作的DbContext类的整个代码
公共类BbContext:IdentityDbContext< IdentityUser>{公共BbContext(DbContextOptions选项):base(选项){Database.EnsureCreated();}公共DbSet< Artist>艺术家{放;}公共DbSet< Lyric>歌词{get;放;}公共DbSet< Heart>心{放;}受保护的重写void OnModelCreating(ModelBuilder builder){builder.Entity< BbUser>(b => b.ToTable("AspNetUsers")));builder.Entity< Heart>().HasKey(h => new {h.UserId,h.LyricId});base.OnModelCreating(builder);}}
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.
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:
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);
}
}
这篇关于带有EF Core Code First和IdentityUser的.Net Core API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!