【问题标题】:Configuring the IdentityModels navigation property with Guid for Entity Framework使用 Guid for Entity Framework 配置 IdentityModels 导航属性
【发布时间】:2022-01-04 22:44:23
【问题描述】:

我开始创建迁移一些类似这样的警告:

外键属性“AppUserClaim.UserId1”是在影子状态下创建的,因为实体类型中存在具有简单名称“UserId”的冲突属性,但未映射、已用于其他关系或不兼容与关联的主键类型。有关 EF Core 中映射关系的信息,请参阅 https://aka.ms/efcore-relationships

它适用于所有具有 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);
            });
}

【问题讨论】:

    标签: entity-framework entity-framework-core entity-framework-6


    【解决方案1】:

    我遇到了类似的问题。在我的 OnModelCreating 方法中,我颠倒了应用迁移的顺序。

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

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

    【讨论】:

      猜你喜欢
      • 2017-12-20
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多