Using Identity with Entity Framework to make one-to-many relationship with other tables

0

In my scenario a user has several boats and a boat has several notes. So I need to relate the user to the boat so that this user can see these notes.

I'm using the MVC 5 default identity template.

So I need to relate the AspNetUser table to the Barco table.

Has anyone ever had to do this so you can help me?

   public class Barco : Entity
   {

    public string Nome { get; private set; }

    public bool Ativo { get; private set; }

    public bool Excluido { get; private set; }

    public int SapId { get; private set; }

    public int CapacidadeAgua { get; private set; }

    public int CapacidadeOleo { get; private set; }

    public int Velocidade { get; private set; }

    public decimal AreaReal { get; private set; }

    public decimal AreaProgramada { get; private set; }

    public decimal AreaLivre { get; private set; }

    public string Email { get; private set; }

    public string Setor { get; private set; }

    public DateTime DataCadastro { get; private set; }



    public Guid? ClasseBarcoId { get; set; }


    public virtual ClasseBarco ClasseBarco { get; set; }




    public Barco(
        String nome, 
        bool ativo, 
        bool excluido, 
        int sapid, 
        int capacidadeAgua,
        int capacidadeOleo,
        int velocidade,
        string email,
        string setor,

        DateTime dataCadastro)
    {
        Nome = nome;
        Ativo = ativo;
        Excluido = excluido;
        SapId = sapid;
        CapacidadeAgua = capacidadeAgua;
        CapacidadeOleo = capacidadeOleo;
        Velocidade = velocidade;
        Email = email;
        Setor = setor;
        DataCadastro = dataCadastro;

        ClasseBarco = new ClasseBarco();
    }
   }
    
asked by anonymous 25.09.2018 / 21:12

1 answer

0

Search for your IdentityModel.cs usually stays within Models/IdentityMode.cs

Inside your class public class ApplicationUser : IdentityUser

Create a new property of type

public virtual Barco Barco {get; set;}

Now in class Boat create an ApplicationUser property

public virtual ApplicationUser ApplicationUser {get; set}

Now in your Application user mapping

do the following

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ApplicationUser>()
    .HasOptional(s => s.Barco)
    .WithRequired(ad => ad.ApplicationUser);

    //isso tem  que vir por ultimo
    base.OnModelCreating(modelBuilder);
}

I did not have a test but I think that should be resolved.

For more information see configuring one to one relationship in asp .net identity

    
25.09.2018 / 21:32