"Aspnetuserlogins"需要定义一个主键(互连的数据库)



我已经从只包含Identity表的数据库中构建了一个DBContext。它是两个DBContext之一,另一个来自另一个数据库,该数据库包含标识数据库中某些列的外键。然而,我得到了一个运行时错误:

实体类型"Aspnetuserlogins"需要定义主键。如果您打算使用无钥匙实体类型,请调用"HasNoKey((">

我有base.OnModelCreating(modelBuilder);行,所以我不知道是什么导致了这个问题。这是我的背景:

public partial class UserContext : IdentityDbContext
{
public UserContext()
{
}
public UserContext(DbContextOptions<UserContext> options)
: base(options)
{
}
public virtual DbSet<Aspnetroleclaims> Aspnetroleclaims { get; set; }
public virtual DbSet<Aspnetroles> Aspnetroles { get; set; }
public virtual DbSet<Aspnetuserclaims> Aspnetuserclaims { get; set; }
public virtual DbSet<Aspnetuserlogins> Aspnetuserlogins { get; set; }
public virtual DbSet<Aspnetuserroles> Aspnetuserroles { get; set; }
public virtual DbSet<Aspnetusers> Aspnetusers { get; set; }
public virtual DbSet<Aspnetusertokens> Aspnetusertokens { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseMySql("UserDB", x => x.ServerVersion("8.0.20-mysql"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Aspnetroleclaims>(entity => ...});
modelBuilder.Entity<Aspnetroles>(entity => ...});
modelBuilder.Entity<Aspnetuserclaims>(entity => ...});
modelBuilder.Entity<Aspnetuserlogins>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey })
.HasName("PRIMARY");
entity.HasIndex(e => e.UserId)
.HasName("IX_AspNetUserLogins_UserId");
entity.Property(e => e.LoginProvider)
.HasCharSet("utf8mb4")
.HasCollation("utf8mb4_0900_ai_ci");
entity.Property(e => e.ProviderKey)
.HasCharSet("utf8mb4")
.HasCollation("utf8mb4_0900_ai_ci");
entity.Property(e => e.ProviderDisplayName)
.HasCharSet("utf8mb4")
.HasCollation("utf8mb4_0900_ai_ci");
entity.Property(e => e.UserId)
.HasCharSet("utf8mb4")
.HasCollation("utf8mb4_0900_ai_ci");
entity.HasOne(d => d.User)
.WithMany(p => p.Aspnetuserlogins)
.HasForeignKey(d => d.UserId)
.HasConstraintName("FK_AspNetUserLogins_AspNetUsers_UserId");
});
modelBuilder.Entity<Aspnetuserroles>(entity => ...});
modelBuilder.Entity<Aspnetusers>(entity => ...});
modelBuilder.Entity<Aspnetusertokens>(entity => ...});
OnModelCreatingPartial(modelBuilder);          
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

这里是aspnetuserlogins模型:

[Table("aspnetuserlogins")]
public partial class Aspnetuserlogins
{
//I tried putting a [Key] here, but got a "use Fluent API error"
[Column(TypeName = "varchar(255)")]
public string LoginProvider { get; set; }
//I tried putting a [Key] here, but got a "use Fluent API error"
[Column(TypeName = "varchar(255)")]
public string ProviderKey { get; set; }
[Column(TypeName = "longtext")]
public string ProviderDisplayName { get; set; }
[Required]
[Column(TypeName = "varchar(255)")]
public string UserId { get; set; }
[ForeignKey(nameof(UserId))]
[InverseProperty(nameof(Aspnetusers.Aspnetuserlogins))]
public virtual Aspnetusers User { get; set; }
}

编辑:我尝试插入一些断点,但在以下行失败:

IList<Timelineinfo> SimilarEvents = _datacontext.Timelineinfo
.Where(t => t.Date.Equals(TempDate))
.ToList();

_datacontext是另一个DbContext,TempDate是一个字符串形式的日期,Timelineinfo看起来像:

[Key]
public int IdTimelineinfo { get; set; }
[Required]       
[MaxLength(10)]
[DataType(DataType.Date)]
[Column(TypeName = "char(10)")]
public string Date { get; set; }
public byte State { get; set; }
Required]
[Column(TypeName = "varchar(20)")]
public string City { get; set; }
public byte Misconduct { get; set; }
public byte? Weapon { get; set; }
[Required]
[Column(TypeName = "mediumtext")]
public string Context { get; set; }
public byte Locked { get; set; }
[Column(TypeName = "varchar(255)")]
public string SubmittedBy { get; set; }
public byte Verified { get; set; }
[ForeignKey(nameof(SubmittedBy))]
[InverseProperty(nameof(Aspnetusers.Timelineinfo))]
public virtual Aspnetusers SubmittedByNavigation { get; set; }

这个问题与这个问题类似,但没有任何答案对有效

问题是因为我试图从两个互连的数据库加载,而没有让EF Core知道数据依赖于用户。问题可以通过将代码更改为以下内容来解决:

public partial class DataContext : UserContext {..}
public partial class UserContext : DbContext
{
public UserContext()
{
}
public UserContext(DbContextOptions options)
: base(options)
{
}
}

相关内容

  • 没有找到相关文章

最新更新