外国钥匙属性'userrole.userid1'是在影子状态创建的 当我尝试添加迁移时,在用户表中创建UserId1和raleid1。 有消息: 外国钥匙属性“ userrole.roleid1”是在阴影状态下创建的,因为一个矛盾的职业...

问题描述 投票:0回答:1

外国密钥属性'userrole.userid1'是在阴影状态下创建的,因为在实体类型中存在具有简单名称'userId'的冲突属性,但未被映射,已经用于另一个关系,或者与该关系不相容关联的主要钥匙类型。
我的型号:

public interface IEntity { public Guid Id { get; set; } } public interface IBaseEntity : IEntity { public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public DateTime? DeletedAt { get; set; } public bool IsDeleted { get; set; } } public class User : IdentityUser<Guid>, IBaseEntity { public string? FirstName { get; set; } public string? LastName { get; set; } public DateTime DateOfBirth { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public DateTime? DeletedAt { get; set; } public bool IsDeleted { get; set; } public bool IsBlocked { get; set; } public virtual ICollection<UserRole>? UserRoles { get; set; } } public class Role : IdentityRole<Guid>, IBaseEntity { public string? Description { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public DateTime? DeletedAt { get; set; } public bool IsDeleted { get; set; } public virtual ICollection<UserRole>? UserRoles { get; set; } } public class UserRole : IdentityUserRole<Guid> { public Guid? UserId { get; set; } public Guid? RoleId { get; set; } public virtual User? User { get; set; } public virtual Role? Role { get; set; } }
我的配置是

public class RoleConfiguration : IEntityTypeConfiguration<Role> { public void Configure(EntityTypeBuilder<Role> builder) { builder .ToTable("Roles"); builder .HasKey(r => r.Id); builder .HasMany(x => x.UserRoles) .WithOne(x => x.Role) .HasForeignKey(x => x.RoleId) .IsRequired(); } } public class UserConfiguration : IEntityTypeConfiguration<User> { public void Configure(EntityTypeBuilder<User> builder) { builder .ToTable("Users"); builder .Property(user => user.FirstName) .HasMaxLength(UserConstants.FirstNameMaxLength); builder .Property(user => user.LastName) .HasMaxLength(UserConstants.LastNameMaxLength); builder .Property(user => user.Email) .HasMaxLength(UserConstants.EmailMaxLength); builder .Property(user => user.PhoneNumber) .HasMaxLength(UserConstants.PhoneMaxLength); builder .HasMany(x => x.UserRoles) .WithOne(x => x.User) .HasForeignKey(x => x.UserId) .IsRequired(); builder .Metadata .RemoveIndex(new[] { builder.Property(x => x.NormalizedUserName).Metadata}); builder .HasIndex(x => new { x.NormalizedUserName }) .HasName("UserNameIndex") .IsUnique(); } } public class UserRoleConfiguration : IEntityTypeConfiguration<UserRole> { public void Configure(EntityTypeBuilder<UserRole> builder) { builder .ToTable("UserRoles"); } }

i试图改变congigs中的关系,删除模型中的接口继承,从userroleth中删除ralesid和userId。

我做错了什么?
    

地图似乎没有任何明显的问题,但是无效的属性和FK可能是一个问题。删除userrole上的无效:
public class UserRole : IdentityUserRole<Guid>
{
    public Guid UserId { get; set; }
    public Guid RoleId { get; set; }

    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}

I还将建议删除Userroles集合上的无效性,并删除Setter访问和初始化的启用:

public virtual ICollection<UserRole> UserRoles { get; } = [];
entity-framework entity-framework-core asp.net-identity entity-framework-migrations
1个回答
0
投票

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. /// <summary> /// Constructor used by EF. /// </summary> protected UserRole() { } #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

您可以使用公共默认构造函数或创建合适的参数化公共构造函数,以确保实体以有效状态构建。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.