Problem in Saving Many to Many Relationship with Core Entity Framework

Asked

Viewed 237 times

0

I’m having trouble saving a relationship from many to many 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 users I get the following Exeception

System.Invalidoperationexception: 'The Property 'Usuarioid' on Entity type 'Usuarioquadro' has a Temporary value. Either set a Permanent value explicitly or ensure that the database is configured to generate values for this Property.'

  • I don’t know if it’s related, but I think in your BaseEntity have a field to ID, maybe the problem is trying to make the composite key .HasKey(uq => new { uq.UsuarioId, uq.QuadroId }); . Try to remove the composite key and leave only the PK

  • in EF Core, you don’t need to put the fields long UsuarioId it recognizes the composition as key automatically, need not even touch the OnModelCreating

  • @Barbetta I did the test but without success, I think you need to map the composite key, because I did the same in the EF Core documentation on the Microsoft website https://docs.microsoft.com/pt-br/ef/core/modeling/relationships

  • @Rovannlinhalis I can remove the properties UsuarioId and QuadroId of the entity UsuarioQuadro and also remove all mapping of the entity UsuarioQuadro in OnModelCreating ?

  • You need to put the code that makes the data fill before saving. Usually if they are new records (you don’t have the ID), you just set the instances in the Usersframework and add them to the list. EF will fill in the Ids after saving Entity.

  • yes, and it seems to me that it is not necessary for Usersframework to inherit Baseentity (which must have a PK), you must define User and Framework as [KEY]

Show 1 more comment

1 answer

0

Based on the error above, I believe you could put in modelBuilder:

modelBuilder.Property(x => x.userId).ValueGeneratedOnAdd();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.