簡體   English   中英

警告:外鍵屬性“ApplicationUserRole.RoleId1”是在影子 state 中創建的

[英]Warning: The foreign key property 'ApplicationUserRole.RoleId1' was created in shadow state

我將身份添加到我的項目中。

楷模:

 [Table("AspNetRoles")]
public class ApplicationRole : IdentityRole
{
    [StringLength(100)]
    public string PersianName { get; set; }

    public string? ParentId { get; set; }

    [ForeignKey("ParentId")]
    public ApplicationRole Parent { get; set; }

    public virtual ICollection<ApplicationUserRole> Users { get; set; }

    public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set; }

    public ApplicationRole()
    {
        Id = Guid.NewGuid().ToString();
    }
    public ApplicationRole(string name, string persianName = "", string parentId = null)
    {
        Name = name;
        PersianName = persianName;
        ParentId = parentId;
    }

}



[Table("AspNetUsers")]
public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        Id = Guid.NewGuid().ToString();
    }
    [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "Required")]
    [Display(Name = "Name", ResourceType = typeof(ApplicationUserResource))]
    [StringLength(50, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "StringLength")]
    public string Name { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "Required")]
    [Display(Name = "Family", ResourceType = typeof(ApplicationUserResource))]
    [StringLength(70, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "StringLength")]
    public string Family { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "Required")]
    [Display(Name = "Email", ResourceType = typeof(ApplicationUserResource))]
    [StringLength(150, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "StringLength")]
    public string Email { get; set; }

    [Display(Name = "Picture", ResourceType = typeof(ApplicationUserResource))]
    public string Picture { get; set; } = "";

    [Display(Name = "Region", ResourceType = typeof(ApplicationUserResource))]
    public Region Region { get; set; }

    [Display(Name = "Language", ResourceType = typeof(ApplicationUserResource))]
    public Language Language { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "Required")]
    [Display(Name = "Mobile", ResourceType = typeof(ApplicationUserResource))]
    [StringLength(12, ErrorMessageResourceType = typeof(ValidationResource), ErrorMessageResourceName = "StringLength")]
    public string Mobile { get; set; }

    public bool AllowDelete { get; set; }
    public bool AllowUpdate { get; set; }

    public Guid CreatedUser { get; set; } = Guid.Empty;

    public DateTime CreatedDate { get; set; } = DateTime.Now;

    public virtual ICollection<ApplicationUserRole> Roles { get; set; }
    public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }


    [NotMapped]
    public string FullName
    {
        get
        {
            var displayName = $"{Name} {Family}";
            return string.IsNullOrWhiteSpace(displayName) ? UserName : displayName;
        }
    }


}

 [Table("AspNetUserRoles")]
public class ApplicationUserRole : IdentityUserRole<string>
{
    
    public virtual ApplicationUser User { get; set; }

    public virtual ApplicationRole Role { get; set; }
}

並在 dbontext 中

 public class ApplicationDbContext : ApplicationApiAuthorizationDbContext<ApplicationUser, ApplicationRole>, IApplicationDbContext
{
    private readonly IMediator _mediator;
    private readonly AuditableEntitySaveChangesInterceptor _auditableEntitySaveChangesInterceptor;

    public ApplicationDbContext(
        DbContextOptions<ApplicationDbContext> options,
        IOptions<OperationalStoreOptions> operationalStoreOptions,
        IMediator mediator,
        AuditableEntitySaveChangesInterceptor auditableEntitySaveChangesInterceptor)
        : base(options, operationalStoreOptions)
    {
        _mediator = mediator;
        _auditableEntitySaveChangesInterceptor = auditableEntitySaveChangesInterceptor;
    }

 protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        builder.Entity<ApplicationUserRole>(userRole =>
        {
            userRole.HasOne(ur => ur.Role)
                .WithMany(r => r.Users)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();

            userRole.HasOne(ur => ur.User)
                .WithMany(r => r.Roles)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });

        base.OnModelCreating(builder);
    }

但是當我添加遷移時。 使用UserId,RoleId,UserId1,RoleId1創建了ApplicationUserRole 並得到警告

外鍵屬性“ApplicationUserRole.RoleId1”是在陰影 state 中創建的,因為實體類型中存在簡單名稱為“RoleId”的沖突屬性,但未映射、已用於其他關系或與關聯的不兼容主鍵類型。 有關 EF Core 中映射關系的信息,請參閱https://aka.ms/efcore-relationships

身份已經定義了那些沒有導航的實體之間的關系。 您正在嘗試使用導航屬性進行配置。

您正在定義與導航的關系,然后調用基本方法。 這將覆蓋您正在使用的 FK 屬性配置,因此您配置的 FK 將 go 到沒有導航的關系。 由於您定義的導航仍在 model 中,約定將為它添加另一個關系並為它們創建影子 FK 屬性。 本質上,Identity 覆蓋了您所做的配置,並且您的導航按照約定導致了額外的關系。 覆蓋身份配置的最佳方法是在編寫自己的配置代碼之前先調用base.OnModelCreating()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM