Mappings in .NET CORE, Fluent API - .HasForeignKey property with error

0

Good afternoon!

I'm starting to develop a system in .NET, sort of in the way, so I'm learning as I'm building, but some errors are still not clear to me.

I have the following code:         // class boat

    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 make this relationship of CategoryBoard with the Boat. For what little do I understand when I do the .HasForeignKey (b => b.BarcoID) it should not find the foreign key of the Boat class that is in the Category class?

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

    
asked by anonymous 20.07.2018 / 20:22

1 answer

0

The problem is in your class CategoryBarco, as it is a 1: N relationship, your class does not have to have the BarcoID property, because if it does, it means you have only one boat (1: 1 ).

Remove the BarcoID property from the CategoryBar class and in the configuration of your class remove this 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();
    
20.07.2018 / 20:34