Code First generating a foreign key
I have the following classes
public class ControleDeVisitas
{
public virtual ICollection<TipoDeSistemas> TipoDeSistemas { get; set; }
}
public class TipoDeSistemas
{
public Guid TipoDeSistemasId { get; set; }
public string Descricao { get; set; }
public DateTime DataCadastro { get; set; }
}
In this way, in the table, TIPO DE SISTEMAS
, a key ControleDeVisitaId
This is the method I use to add a TipoDeSistema
to Controle
public ControleDeVisitasViewModel Adicionar(ControleDeVisitasViewModel controleDeVisitasViewModel, List<Guid> sistemasComerciais)
{
var controle = Mapper.Map<ControleDeVisitas>(controleDeVisitasViewModel);
foreach (var sistema in sistemasComerciais)
{
var ts = _tipoDeSistemaRepositorio.BuscarPorId(sistema);
controle.TipoDeSistemas.Add(ts);
}
_controleDeVisitaRepositorio.Adicionar(controle);
}
And this is how thebelah is getting TipoDeSistemas
I deleted some data, but the truth is that it is duplicating the same information, like, if I choose the system A, B and C. (Both must already be registered).
It creates a new system Type A, B and C in the same table. and put the control key.
Does anyone know to tell me because he is creating this key in Table TipoDeSistema
?
So is my config:
public ControleDeVisitasConfig()
{
ToTable("ControleDeVisitas");
HasKey(c => c.ControleDeVisitasId);
HasMany(c => c.TipoDeSistemas);
}
1
Author: Rafael Passos, 2019-09-12
1 answers
Well, in this case Entity Framework
is doing what it was told. That is, a relationship 1: n .
So there is a need for Visit control to "know" how to relate to many types of systems.
1
Author: Renan Carlos, 2019-09-13 12:08:18