Code First Generating a foreign key

Asked

Viewed 32 times

1

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; }
 }

thus, in the table, TIPO DE SISTEMAS, a key is being created ControleDeVisitaId

this is the method I use to add a TipoDeSistema at the 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 that’s how the tebela is getting TipoDeSistemas inserir a descrição da imagem aqui

I erased some data, but the truth is, it’s duplicating the same information, like, if I choose system A, B and C.(both should already be registered).

it creates a new system of type A, B and C in the same table. and puts the Control key.

someone knows me why he is creating this key in the table TipoDeSistema?

so is my config:

 public ControleDeVisitasConfig()
    {
            ToTable("ControleDeVisitas");
            HasKey(c => c.ControleDeVisitasId);
            HasMany(c => c.TipoDeSistemas);
    }

1 answer

1


Well, in this case the Entity Framework is doing what was told to him. Or is it a relationship 1:N.

So there is a need for visit control to "know" how to relate to many types of systems.

Browser other questions tagged

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