EF Core 无法一对多保存相关数据

EF Core Cannot Save Related Data one to many

我正在尝试将新用户保存到具有新角色的用户 table 中。这是型号和配置。

当我插入以下实体时,角色 table 会填充新角色,但不会插入用户实体。

    `var userEntity = new User
    {
        Id = 0,
        Active = true,
        Firstname = "Fname",
        Lastname = "Lname",
        Password = "test",
        Phonenumber = "2223334444",
        Test = false,
        Username = "test@test.com",
        RoleId = 0
        //                Role = roleEntity
    };
    var roleEntity = new Role
    {
        Id = 0,
        Name = "test role",
        Active = true,
        Users = new List<User>
        {
            userEntity
        }
    };

    context.Role.Add(roleEntity);
    await context.SaveChangesAsync();

public class User
{
    public int Id { get; set; }

    public string Username { get; set; }

    public string Password { get; set; }

    public string Firstname { get; set; }

    public string Middlename { get; set; }

    public string Lastname { get; set; }

    public string Phonenumber { get; set; }

    public bool Active { get; set; }

    public bool Test { get; set; }

    public int RoleId { get; set; }


    public virtual Role Role { get; set; }
}

public class Role
{
    public int Id { get; set; }

    public string Name { get; set; }

    public bool Active { get; set; }

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

    public Role()
    {
        Users = new HashSet<User>();
    }
}`

这是实体配置。

`

 public class RoleConfiguration : IEntityTypeConfiguration<Role>
 {
    public void Configure(EntityTypeBuilder<Role> builder)
    {
        builder.ToTable("Role");

        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).IsRequired().ValueGeneratedOnAdd();
        builder.Property(r => r.Name).IsRequired().HasMaxLength(255);
        builder.Property(r => r.Active).IsRequired();
        builder.HasMany(r => r.Users)
            .WithOne(u => u.Role)
            .HasForeignKey(u => u.RoleId)
            .OnDelete(DeleteBehavior.Restrict);

    }
}

    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable("User");

        builder.HasKey(u => u.Id);
        builder.Property(u => u.Id).IsRequired().ValueGeneratedOnAdd();
        builder.Property(u => u.Username).IsRequired().HasMaxLength(255);
        builder.Property(u => u.Password).IsRequired();
        builder.Property(u => u.Firstname).IsRequired().HasMaxLength(255);
        builder.Property(u => u.Lastname).IsRequired().HasMaxLength(255);
        builder.Property(u => u.Phonenumber).HasMaxLength(255);
        builder.Property(u => u.Active).IsRequired();
        builder.Property(u => u.Test).IsRequired();
        builder.Property(u => u.RoleId).IsRequired();

        builder.HasOne(u => u.Role)
            .WithMany(r => r.Users)
            .HasForeignKey(u => u.RoleId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}`

有没有哪里做错了。

感谢您的帮助。

我用错了

context.Entry(entity).State = EntityState.Added;

而不是

context.Add(entity);

这就是没有添加相关实体的原因。