0
I have a relationship N:N between Activity and Project, where an activity has many projects:
public class Atividade : ObjetoPersistente
{
public Atividade()
{
StatusAtividade = EStatusAtividade.NaoIniciado;
TipoAtividade = ETipoAtividade.NovaImplementacao;
Usuario = new Usuario();
Projetos = new List<Projeto>();
}
[JsonConverter(typeof(FormatoDataHoraMinutoNullableConverter))]
public DateTime? DataHoraFim { get; set; }
[JsonConverter(typeof(FormatoDataHoraMinutoNullableConverter))]
public DateTime? DataHoraInicio { get; set; }
public string DescricaoAtividade { get; set; }
public string EstimativaInicialAtividade { get; set; }
public Usuario Usuario { get; set; }
public string LoginUsuario
{
get { return Usuario.Login; }
}
public EStatusAtividade StatusAtividade { get; set; }
public string DescricaoStatusAtividade
{
get { return StatusAtividade.Descricao; }
}
public string DescricaoTipoAtividade
{
get { return TipoAtividade.Descricao; }
}
public ETipoAtividade TipoAtividade { get; set; }
public string TituloAtividade { get; set; }
public long CodigoUsuario
{
get { return Usuario.Codigo; }
set { Usuario.Codigo = value; }
}
public List<Projeto> Projetos { get; set; }
public List<long> CodigoProjetos
{
get { return ObtenhaListaDeCodigosPorListaDeObjetos(Projetos); }
set { Projetos = ObtenhaListaDeObjetoPorListaDeCodigos<Projeto>(value); }
}
public override bool Equals(object obj)
{
return (obj is Atividade) && (obj as Atividade).Codigo.Equals(Codigo);
}
public override int GetHashCode()
{
return Codigo.GetHashCode();
}
}
Project:
public class Projeto : ObjetoPersistente, IObjetoElementoOption
{
public string Nome { get; set; }
public override bool Equals(object obj)
{
return (obj is Projeto) && (obj as Projeto).Codigo.Equals(Codigo);
}
public override int GetHashCode()
{
return Codigo.GetHashCode();
}
public string Valor
{
get { return Codigo.ToString(); }
}
public string Descricao {
get { return Nome; }
}
}
The mappings are this way:
public AtividadeMap()
{
HasKey(a => a.Codigo);
ToTable("tb_atividade");
Property(a => a.TituloAtividade).HasColumnName("titulo_atividade");
Property(a => a.DescricaoAtividade).HasColumnName("descricao_atividade");
Property(a => a.DataHoraInicio).HasColumnName("data_hora_inicio");
Property(a => a.DataHoraFim).HasColumnName("data_hora_fim");
Property(a => a.Codigo).HasColumnName("pk_atividade");
Property(a => a.StatusAtividade.Identificador).HasColumnName("status_atividade");
Property(a => a.EstimativaInicialAtividade).HasColumnName("estimativa_inicial_atividade");
Property(a => a.TipoAtividade.Identificador).HasColumnName("tipo_atividade");
Property(a => a.CodigoUsuario).HasColumnName("fk_usuario");
HasRequired(a => a.Usuario)
.WithMany()
.HasForeignKey(a => a.CodigoUsuario);
HasMany(a => a.Projetos)
.WithMany()
.Map(pa =>
{
pa.MapLeftKey("fk_atividade");
pa.MapRightKey("fk_projeto");
pa.ToTable("tb_atividade_projeto");
});
}
public class ProjetoMap : EntityTypeConfiguration<Projeto>
{
public ProjetoMap()
{
HasKey(a => a.Codigo);
ToTable("tb_projeto");
Property(p => p.Codigo).HasColumnName("pk_projeto");
Property(a => a.Nome).HasColumnName("nome");
}
}
The consultation process is OK, however, inclusions and changes do not work.
What is missing?
After researching the subject, I found several answers like this, which in short, in my situation, I would have to carry each Project of the object list Activity to accomplish a Attach in the context class:
Is this the only way to update the relationship situation Many-to-Many? Could it make the persistence of these relationships generic?
Yeah, it’s confusing at first. You need to recover the objects and give attach, if you do not and try to add, the framework will probably create NEW objects instead of using the old ones. If you get what you needed, summarize what you needed as an answer. This question is very important.
– RSinohara
@Rsinohara actually I’ve seen working this way, however, I’m interested in more simplified and optimized ways to do this.
– Joaquim Magalhães
I get it... I spent a lot of time getting used to the way this works. And I wrote gigantic codes along the way.
– RSinohara
I was going to try to answer, but I didn’t understand any of your question. I don’t even know where is the relation N to N.
– Leonel Sanches da Silva
@Joaquimmagalhães The relationship you describe in the code is 1:N. I was going to suggest an edit on the question and the title, but I thought it would be a very 'heavy' change. But I think you should change.
– RSinohara
As @Ciganomorrisonmendez noted, my code visualizes the relation N:N in a different way from the conventions of N:N of EF. But it’s an N:N relationship, just as I exemplified in an analogy in his reply.
– Joaquim Magalhães