我从一个空的 asp.net core 2 项目开始,想向它添加功能,以便我可以学习所有部分。我在 Azure 中创建了一个要连接的数据库,并且我的项目中有一个 dbContext。我正在使用 ASP.NET Core 标识,并且已成功将表添加到应用程序中。我正在尝试对我的数据库上下文进行一些更改并将其推送到我的数据库中。这就是我所理解的,需要在 EF Core 中使用“代码优先”来完成。当我尝试在 PMC 中创建新迁移时,出现以下错误。我不知道这是什么意思。对此有什么想法吗?我该如何解决这个问题?
TIA
错误信息:
不能将表 'AspNetRoleClaims' 用于实体类型 'AspNetRoleClaims',因为它用于实体类型 'IdentityRoleClaim' 并且主键 {'Id'} 和主键 {'Id'} 之间没有关系。
我的数据库上下文的来源:
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace PooperApp.Models
{
public partial class PoopTheWorldContext : Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
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; }
public virtual DbSet<Clean> Clean { get; set; }
public virtual DbSet<Country> Country { get; set; }
public virtual DbSet<PoopLocation> PoopLocation { get; set; }
public virtual DbSet<Province> Province { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer(@"......");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AspNetRoleClaims>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.RoleId);
entity.HasOne(d => d.Role)
.WithMany(p => p.AspNetRoleClaims)
.HasForeignKey(d => d.RoleId);
});
modelBuilder.Entity<AspNetRoles>(entity =>
{
entity.HasIndex(e => e.NormalizedName)
.HasName("RoleNameIndex")
.IsUnique()
.HasFilter("([NormalizedName] IS NOT NULL)");
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Ipaddress).HasColumnName("IPAddress");
entity.Property(e => e.Name).HasMaxLength(256);
entity.Property(e => e.NormalizedName).HasMaxLength(256);
});
modelBuilder.Entity<AspNetUserClaims>(entity =>
{
entity.HasIndex(e => e.UserId);
entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserClaims)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AspNetUserLogins>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey });
entity.HasIndex(e => e.UserId);
entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserLogins)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AspNetUserRoles>(entity =>
{
entity.HasKey(e => new { e.UserId, e.RoleId });
entity.HasIndex(e => e.RoleId);
entity.HasOne(d => d.Role)
.WithMany(p => p.AspNetUserRoles)
.HasForeignKey(d => d.RoleId);
entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserRoles)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AspNetUsers>(entity =>
{
entity.HasIndex(e => e.NormalizedEmail)
.HasName("EmailIndex");
entity.HasIndex(e => e.NormalizedUserName)
.HasName("UserNameIndex")
.IsUnique()
.HasFilter("([NormalizedUserName] IS NOT NULL)");
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Email).HasMaxLength(256);
entity.Property(e => e.NormalizedEmail).HasMaxLength(256);
entity.Property(e => e.NormalizedUserName).HasMaxLength(256);
entity.Property(e => e.UserName).HasMaxLength(256);
});
modelBuilder.Entity<AspNetUserTokens>(entity =>
{
entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name });
entity.HasOne(d => d.User)
.WithMany(p => p.AspNetUserTokens)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<Clean>(entity =>
{
entity.Property(e => e.DateEntered).HasColumnType("datetime");
entity.Property(e => e.DateUpdated).HasColumnType("datetime");
entity.Property(e => e.Description).HasMaxLength(512);
entity.Property(e => e.Name).HasMaxLength(50);
});
modelBuilder.Entity<Country>(entity =>
{
entity.Property(e => e.DateEntered).HasColumnType("datetime");
entity.Property(e => e.DateUpdated).HasColumnType("datetime");
entity.Property(e => e.Description).HasMaxLength(512);
entity.Property(e => e.Name).HasMaxLength(50);
entity.Property(e => e.AppleName).HasMaxLength(50);
entity.Property(e => e.GoogleName).HasMaxLength(50);
});
modelBuilder.Entity<PoopLocation>(entity =>
{
entity.HasIndex(e => e.AspNetUsersId);
entity.HasIndex(e => e.PostalCode);
entity.HasIndex(e => e.ProvinceId);
entity.Property(e => e.Address1).HasMaxLength(100);
entity.Property(e => e.Address2).HasMaxLength(100);
entity.Property(e => e.City).HasMaxLength(100);
entity.Property(e => e.DateEntered).HasColumnType("datetime");
entity.Property(e => e.DateUpdated).HasColumnType("datetime");
entity.Property(e => e.PostalCode).HasMaxLength(50);
entity.HasOne(d => d.AspNetUsers)
.WithMany(p => p.PoopLocation)
.HasForeignKey(d => d.AspNetUsersId)
.HasConstraintName("FK_PoopLocation_AspNetUsersId");
entity.HasOne(d => d.Clean)
.WithMany(p => p.PoopLocation)
.HasForeignKey(d => d.CleanId)
.HasConstraintName("FK_PoopLocation_CleanId");
entity.HasOne(d => d.Province)
.WithMany(p => p.PoopLocation)
.HasForeignKey(d => d.ProvinceId)
.HasConstraintName("FK_PoopLocation_ProvinceId");
});
modelBuilder.Entity<Province>(entity =>
{
entity.HasIndex(e => e.CountryId);
entity.Property(e => e.Abbreviation).HasMaxLength(5);
entity.Property(e => e.AppleName).HasMaxLength(50);
entity.Property(e => e.DateEntered).HasColumnType("datetime");
entity.Property(e => e.DateUpdated).HasColumnType("datetime");
entity.Property(e => e.Description).HasMaxLength(512);
entity.Property(e => e.GoogleName).HasMaxLength(10);
entity.Property(e => e.Name).HasMaxLength(50);
entity.HasOne(d => d.Country)
.WithMany(p => p.Province)
.HasForeignKey(d => d.CountryId)
.HasConstraintName("FK_Province_Country");
});
}
}
}