使用 Entity Framework 的 Guid 配置 IdentityModels 导航 属性

Configuring the IdentityModels navigation property with Guid for Entity Framework

我开始创建 Migration 一些像这样的警告:

The foreign key property 'AppUserClaim.UserId1' was created in shadow state because a conflicting property with the simple name 'UserId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.

它适用于具有 AppUser 导航的所有实体 属性。其他导航属性没有警告。

public class AppUser : IdentityUser<Guid>, IChangeTrackerObject
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        [Column(TypeName = "text")]
        public string ProfilePictureDataUrl { get; set; }

        public string CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }

        public string ChangedBy { get; set; }
        public DateTime? ChangedOn { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime? DeletedOn { get; set; }
        public bool IsActive { get; set; }
        public string RefreshToken { get; set; }
        public DateTime RefreshTokenExpiryTime { get; set; }

        public virtual ICollection<AppUserClaim> Claims { get; set; }
        public virtual ICollection<AppUserLogin> Logins { get; set; }
        public virtual ICollection<AppUserToken> Tokens { get; set; }
        public virtual ICollection<AppUserRole> UserRoles { get; set; }

        public AppUser()
        {
            
        }
    }
public class AppUserClaim : IdentityUserClaim<Guid>, IChangeTrackerObject
    {
        public string ChangedBy { get; set; }
        public DateTime? ChangedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }

        public virtual AppUser User { get; set; }
    }

private static void BuildIdentity(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AppUser>(entity =>
            {
                entity.ToTable(name: "Users", schema);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();

                // Each User can have many UserClaims
                entity.HasMany(e => e.Claims)
                    .WithOne()
                    .HasForeignKey(uc => uc.UserId)
                    .IsRequired();

                // Each User can have many UserLogins
                entity.HasMany(e => e.Logins)
                    .WithOne()
                    .HasForeignKey(ul => ul.UserId)
                    .IsRequired();

                // Each User can have many UserTokens
                entity.HasMany(e => e.Tokens)
                    .WithOne()
                    .HasForeignKey(ut => ut.UserId)
                    .IsRequired();

                // Each User can have many entries in the UserRole join table
                entity.HasMany(e => e.UserRoles)
                    .WithOne()
                    .HasForeignKey(ur => ur.UserId)
                    .IsRequired();

            });
            modelBuilder.Entity<AppUserClaim>(entity =>
            {
                entity.ToTable("UserClaims", schema);
            });
}

我运行遇到了类似的问题。在我的 OnModelCreating 方法中,我翻转了应用迁移的顺序。

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}

基本上,如果在调用我的代码应用配置之后调用基本方法,那么基本方法中的配置将覆盖我的配置,这给了我一个与你类似的错误有。所以我假设正在发生的事情是你在 之后调用 BuildIdentity() 你调用 base.OnModelCreating()。您可能需要颠倒该顺序,否则默认身份数据库中定义的关系可能优先。