Maps in . NET CORE, Fluent API - property . Hasforeignkey with error

Asked

Viewed 44 times

0

Good afternoon!

I’m starting to develop a system in. net, kind of in Brazil, so I’m learning as I build, but some mistakes are not yet clear for me

I have the following code: //boat class

    public string Nome { get; private set; }

    public bool Ativo { get; private set; }

    public string SapId { get; private set; }

    public string Resumo { get; private set; }

    public DateTime DataCadastro { get; private set; }

    public Nullable<int> CapacidadeAgua { get; private set; }

    public Nullable<int> CapacidadeOleo { get; private set; }

    public Nullable<int> Velocidade { get; private set; }

    //Chave estrangeira
    public Guid CategoriaBarcoID { get; private set; }
     //Chave estrangeira
    public Guid TipoOperacaoID { get; private set; }



    //Entity Framework propriedades de Navegação
    public virtual CategoriaBarco CategoriaBarco { get; private set; }

    public virtual TipoOperacao TipoOperacao { get; private set; }

////////////////////////////////////////////////////////////////////////////

public class CategoriaBarco : Entity<CategoriaBarco>
{

    public CategoriaBarco(Guid id, Guid BarcoId, string nome, bool ativo)
    {
        Id = id;
        Nome = nome;
        Ativo = ativo;
    }

    //Construtor para o Entityframework
    protected CategoriaBarco()
    {

    }

    public string Nome { get; private set; }

    public bool Ativo { get; private set; }

    //Chave estrangeira
    public Guid BarcoId { get; set; }


    //Entity Framework Propeidades de Navegação
    public virtual ICollection<Barco> Barcos { get; private set; }



//mapeamento entre a classe Barco e CategoriaBarco
modelBuilder.Entity<Barco>()
           .HasOne(b => b.CategoriaBarco)
           .WithMany(c => c.Barcos)
           .HasForeignKey(b => b.CategoriaBarcoID)
           .IsRequired();

        modelBuilder.Entity<Barco>()
            .HasOne(b => b.TipoOperacao)
            .WithMany(b => b.barcos)
            .HasForeignKey(b => b.TipoOperacaoID)
            .IsRequired();

       //Mapeamento entre CategoriabBarco e Barcos
      //Código com Erro
          modelBuilder.Entity<CategoriaBarco>()
           .HasMany(c => c.Barcos)
           .WithOne(b => b.CategoriaBarco)
           .HasForeignKey (b => b.BarcoID )
           .IsRequired();

For some reason I can not do this relationship of Categoriabarco with Barco. By the little q understood when I do the . Hasforeignkey (b => b.Barcoid ) it should not find the foreign key of the Boat class that is in the Class category?

How could you make this relationship where, a category has many boats, but a boat only has one category?

1 answer

0


The problem is in your Categoriabarco class, as it is a 1:N ratio, your class does not need to have the Barcoid property, because if it does, it means that you are saying that Categoria has only one boat (1:1).

Remove the Barcoid property from the Class Categoriabarco and in your class configuration remove that part:

//Mapeamento entre CategoriabBarco e Barcos
//Código com Erro
modelBuilder.Entity<CategoriaBarco>()
    .HasMany(c => c.Barcos)
    .WithOne(b => b.CategoriaBarco)
    .HasForeignKey (b => b.BarcoID )
    .IsRequired();
  • But then, in the Categoriabarco class I have this property: public virtual Icollection<Barco> Boats { get; private set; }, which would be the collection of boats that a category could have. This Barcoid would be the foreign key of the class boat, it is only a property of the Type Guid

  • But it’s wrong dude, every boat has the id, in a 1:N relationship the FK stays in the daughter entity (boat). Do you realize that if you have a list of boats, it doesn’t make sense that you only have the ID of 1?

  • I understood, how I already mapped 1:N Hasone(b => b.Categoriabarco) . Withmany(c => c.Boats) . Hasforeignkey(b => b.Categoriabarcoid) is already understood that the Categoriabarco already has many Boats and the foreign key that is in the Boat class, will make identification.

  • Exactly, in Entity you only need to configure one of the entities when it comes to relationships, of course there are exceptions, but in common cases this is the step. : ) If the answer has helped, mark the answer as accepted, so that others can benefit from the answer

Browser other questions tagged

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