I'm having trouble saving a many-to-many relationship with Entity Framework Core
I have the following entities
[Table("quadros")]
public class Quadro : BaseEntity
{
public bool Ativo { get; set; }
public string Titulo { get; set; }
public List<CheckList> Checklists { get; set; }
public bool Concluido { get; set; }
public DateTime DataUltimaOperacao { get; set; }
public List<UsuarioQuadro> Usuarios { get; set; }
public long EmpresaId { get; set; }
public Empresa Empresa { get; set; }
public List<Atividade> Atividades { get; set; }
}
[Table("usuarios")]
public class Usuario : BaseEntity
{
public bool Ativo { get; set; }
public string Nome { get; set; }
public string SobreNome { get; set; }
public string Email { get; set; }
public string Login { get; set; }
public string Senha { get; set; }
public int TipoUsuario { get; set; }
public long EmpresaId { get; set; }
public Empresa Empresa { get; set; }
public List<UsuarioQuadro> Quadros { get; set; }
public List<Atividade> Atividades { get; set; }
}
Relationship entity
[Table("usuarioQuadro")]
public class UsuarioQuadro : BaseEntity
{
public Usuario Usuario { get; set; }
public long UsuarioId { get; set; }
public Quadro Quadro { get; set; }
public long QuadroId { get; set; }
}
My DataContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UsuarioQuadro>()
.HasKey(uq => new { uq.UsuarioId, uq.QuadroId });
modelBuilder.Entity<UsuarioQuadro>()
.HasOne(u => u.Usuario)
.WithMany(uq => uq.Quadros)
.HasForeignKey(u => u.UsuarioId)
modelBuilder.Entity<UsuarioQuadro>()
.HasOne(q => q.Quadro)
.WithMany(uq => uq.Usuarios)
.HasForeignKey(u => u.QuadroId);
}
But when I save the frame with the users I get the following Exeception
System.InvalidOperationException: 'The property' UserId 'on entity type' UserAccount 'has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property. '