我有一个在.Net Framework中的项目。我使用.Net Framework和.Net Framework Asp.net Identity。这里没有问题...
现在我正在创建一个新的项目,这是一个.Net Core 2.2项目,必须连接到现有的项目。这是一个.Net Core 2.2项目,必须连接到现有的数据库。我不能改变数据库的结构。我已经用这个命令把数据库架起来了。
Scaffold-DbContext -Connection "..." -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDatabaseContext -Tables "Profile", <other_tables_no_identity>
所以数据库已经搭好了脚手架,然后我手动修改了以下文件。
IdentityUser
到 Profile
实体 public partial class Profile : IdentityUser {...}
OnModelCreating
以这种方式。 protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//I have added this lines
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>(entity =>
{
entity.ToTable("BaseProfile");
entity.Property(e => e.Id)
.HasMaxLength(128)
.ValueGeneratedNever();
entity.Property(e => e.Email)
.IsRequired()
.HasMaxLength(256);
entity.Property(e => e.UserName)
.IsRequired()
.HasMaxLength(256);
});
modelBuilder.Entity<Profile>(entity =>
{
entity.ToTable("Profile");
entity.HasIndex(e => e.Id)
.HasName("IX_Id");
entity.Property(e => e.Id)
.HasMaxLength(128)
.ValueGeneratedNever();
entity.Property(e => e.CreationDate).HasColumnType("datetime");
entity.Property(e => e.LastAccessDate).HasColumnType("datetime");
entity.Property(e => e.Rating).HasColumnType("decimal(18, 2)");
});
modelBuilder.Entity<IdentityUserRole<string>>().ToTable("ProfileRoleAssociation");
modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("ProfileLoginAssociation");
modelBuilder.Entity<IdentityUserClaim<int>>().ToTable("ProfileClaimAssociation");
modelBuilder.Entity<IdentityRole>().ToTable("ProfileRole");
}
我从旧的.Net框架项目中复制了这几行代码... ... 但是当我尝试登录时,我得到了这个错误。
在处理请求时发生了一个未处理的异常。 SqlException.Invalid column name 'NormalizedUs': Invalid column name 'NormalizedUserName'. 无效列名'AccessFailedCount'。 无效列名'ConcurrencyStamp'。 无效的列名'Email'.SqlException: Invalid column name 'NormalizedUserName'.无效的列名'AccessFailedCount'.无效的列名'ConcurrencyStamp'. 无效列名'EmailConfirmed'。 Invalid column name 'LockoutEnabled'. Invalid column name 'LockoutEnd'.无效列名 'NormalizedEmail'. 无效列名'NormalizedEmail'。 无效列名'NormalizedUserName'。 无效列名'PasswordHash'。 无效列名'电话号码'。 无效列名'PhoneNumberConfirmed'。 无效列名'SecurityStamp'。 无效列名'TwoFactorEnabled'。 无效的列名'UserName'。
列名 NormalizedUserName
并不存在。但其他的是的。
怎么了?谢谢你
编辑
我理解的问题是关于继承的。它寻找里面的列 Profile
桌子而不是 BaseProfile
...但我仍然不明白如何解决它。
这有点复杂,因为我不能修改原来的.net框架应用程序...。
这里的情况在旧的.net框架项目:我有一个...... profile
类继承自 IdentityUser
.
public class Profile : IdentityUser { ... }
那么 OnModelCreating
我有这个
var baseProfile = modelBuilder.Entity<IdentityUser>().ToTable("BaseProfile");
modelBuilder.Entity<Profile>().ToTable("Profile");
modelBuilder.Entity<IdentityUserRole>().ToTable("ProfileRoleAssociation");
modelBuilder.Entity<IdentityUserLogin>().ToTable("ProfileLoginAssociation");
modelBuilder.Entity<IdentityUserClaim>().ToTable("ProfileClaimAssociation");
modelBuilder.Entity<IdentityRole>().ToTable("ProfileRole");
所以,这是我无法改变的初始情况。
好了,在我的新的.net core项目中,我已经启动了这个命令在 PackageManager Console
:
Scaffold-DbContext -Connection {connection_string} -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyContext -Tables "Profile", "BaseProfile", <other_tables>
说明: BaseProfile
并不是.net框架中的实体。请注意,由于某些原因,该命令没有生成在 IdentityUser
类。所以,我修改了自动生成的类,如下。
public partial class BaseProfile : IdentityUser
{
public override string Id { get; set; }
public override string Email { get; set; }
public override bool EmailConfirmed { get; set; }
public override string PasswordHash { get; set; }
public override string SecurityStamp { get; set; }
public override string PhoneNumber { get; set; }
public override bool PhoneNumberConfirmed { get; set; }
public override bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public override bool LockoutEnabled { get; set; }
public override int AccessFailedCount { get; set; }
public override string UserName { get; set; }
public override string NormalizedEmail { get { return this.Email.ToUpperInvariant(); } }
public override string NormalizedUserName { get { return this.UserName.ToUpperInvariant(); } }
public virtual Profile Profile { get; set; }
}
我修改了 BaseProfile
使其继承了 IdentityUser
. 所有的属性都是由脚手架命令自动生成的,并定义在 IdentityUser
类已被覆盖。不要删除它们,即使它们是在 IdentityUser
我还创建了两个新的属性,这些属性在旧版MS Identity中是不存在的。
public override string NormalizedEmail { get { return this.Email.ToUpperInvariant(); } }
public override string NormalizedUserName { get { return this.UserName.ToUpperInvariant(); } }
Normalized*
这些属性在MS Identity的旧版本中并不存在。但它们在新版本中是必要的。这两个属性需要让用户登录。事实上,用户在登录时的输入是与这两个属性进行比较的。好吧,这不是最正确的属性实现。但是对于我的文化来说是可以的。
最后,我想说的是 OnModelCreating
我添加了这几行。
modelBuilder.Entity<BaseProfile>(entity =>
{
entity.ToTable("BaseProfile");
entity.Ignore(e => e.LockoutEnd);
entity.Ignore(e => e.ConcurrencyStamp);
entity.Ignore(e => e.NormalizedEmail);
entity.Ignore(e => e.NormalizedUserName);
entity.Ignore(e => e.LockoutEnd);
});
这些都是旧版个人信息中没有的属性,只有在新版中才有。