Using Identity with Entity Framework to relate one-to-Many methods to other tables

Asked

Viewed 206 times

0

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

I’m using the MVC 5 project’s default Identity template.

Therefore, I need to relate the Aspnetuser table to the Barco table.

Someone’s already had to do it so they 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();
    }
   }

1 answer

0

Look for your Identitymodel.Cs is usually within Models/IdentityMode.cs

Within your class public class ApplicationUser : IdentityUser

Create a new property of type

public virtual Barco Barco {get; set;}

Now in the Barco class create an Applicationuser property

public virtual ApplicationUser ApplicationUser {get; set}

Now on 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 had no way to test it but I think it should solve.

for more information see Configuring one to one Relationship in Asp.net Identity

Browser other questions tagged

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