我将我的应用程序从.Net Core 1.1,EF Core 1.1,ASP.Net Identity 1.1迁移到所有版本2.1。我还从IdentityUser中分离了自己的用户(当前称为Diver
),因此不需要为ASP.Net Idenentity-EF提供自定义用户类。
当我现在尝试使用以下方法添加初始迁移时:
dotnet ef migrations add initial -s . -p ../Tauchbolde.Common/
我得到了错误:
The entity type 'IdentityUserLogin<int>' requires a primary key to be defined.
我的上下文如下:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{ }
public DbSet<Event> Events { get; set; }
public DbSet<Participant> Participants { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Diver> Diver { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<PostImage> PostImages { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// ApplicationUser
builder.Entity<Diver>()
.HasMany(e => e.Notificationses)
.WithOne(e => e.Recipient)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Diver>()
.HasMany(e => e.Comments)
.WithOne(e => e.Author)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Diver>()
.HasMany(e => e.Events)
.WithOne(e => e.Organisator)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Diver>()
.HasMany(e => e.Posts)
.WithOne(e => e.Author)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Diver>()
.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Diver>()
.HasMany(e => e.Logins)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Diver>()
.HasMany(e => e.Roles)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
// Comment
builder.Entity<Comment>().HasOne(e => e.Event).WithMany(e => e.Comments).OnDelete(DeleteBehavior.Restrict);
// Event
builder.Entity<Event>().HasIndex(p => new { p.StartTime, p.Deleted });
builder.Entity<Event>().Property(e => e.Deleted).HasDefaultValue(false);
builder.Entity<Event>().Property(e => e.Canceled).HasDefaultValue(false);
// Notification
builder.Entity<Notification>().Property(e => e.AlreadySent).HasDefaultValue(false);
builder.Entity<Notification>().Property(e => e.CountOfTries).HasDefaultValue(0);
// Participants
builder.Entity<Participant>().HasOne(e => e.Event).WithMany(e => e.Participants).OnDelete(DeleteBehavior.Restrict);
builder.Entity<Participant>().Property(e => e.CountPeople).HasDefaultValue(1);
// Post
builder.Entity<Post>().HasIndex(p => new { p.Category, p.PublishDate });
// PostImage
// UserInfo
builder.Entity<Diver>().Property(e => e.NotificationIntervalInHours).HasDefaultValue(1);
}
完整的源代码可以在GitHub上找到。
我已经尝试了很多事情,例如为Identity实体添加ignore语句。什么都没做,我被困住了。
有什么想法会出问题吗?
答案 0 :(得分:0)
我不确定,但是尝试用此替换您的第一行:
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
答案 1 :(得分:0)
似乎我的实体与ASP.Net Identity实体之间存在一些关系。切换到继承的Identity用户实体时,似乎没有清理它们。我还向DBContext添加了两个构造函数,它们不使用通用参数,而是使用默认参数。这已经解决了我现在可以通过问题中显示的推荐添加迁移的方式。