Associate Identity Applicationuser to my Personal Class

Asked

Viewed 383 times

1

My Asp.Net MVC system uses Identity in its default form, with a few simple customizations. I also have a table of Individuals (which inherits some information from People, but I don’t think this is the case).

I would like to associate the Applicationuser with Personal Physique, so that every Applicationuser has a Personal Physique (Personal Physique may or may not have an Applicationuser).

public class ApplicationUser : IdentityUser
{
   [ForeignKey("PessoaFisica")]
   public int PessoaFisicaId { get; set; }

   public virtual PessoaFisica PessoaFisica { get; set; }

   public async Task GenerateUserIdentityAsync(UserManager manager)
   {
      ...
    }
}

In the Personal Physics class I have the following:

[Table("Pessoas")]
public partial class PessoaFisica : Pessoa
{
   public int Id { get; set; }

   public string Nome { get; set; }

   ...

   public virtual ApplicationUser Usuario { get; set; }
}

The mistake I get is:

One or more validation errors Were Detected During model Generation:

Applicationuser_people_source: Multiplicity is not Valid in Role 'Applicationuser_people_source' in Relationship 'Applicationuser_pessoafisica'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

Placing the relationship with Fluent API in the model:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity‹PessoaFisica›()
   .HasOptional(f => f.Usuario)
   .WithRequired(s => s.PessoaFisica);
}

I have a mistake too, but it’s another:

Unable to determine the main end of an Association between the types 'Webapplication5.Models.Pessoafisica' and 'Webapplication5.Models.Applicationuser'. The main end of this Association must be explicitly configured using either the Relationship Fluent API or data Annotations.

What I’m doing wrong?

  • Your problem there is that several individuals may refer to the same Applicationuser.

1 answer

0

I found it very complicated to implement this. Many errors related to Identity.

I ended up making a relationship medium to form, consulting my table each time I want to consult the user of a physical person. The same I do to consult the physical person of a user. I hope you help someone.

Applicationuser class:

public class ApplicationUser : IdentityUser
{
   public int PessoaFisicaId { get; set; }
   public virtual PessoaFisica PessoaFisica
   {
      get
      {
         MinhaModel db = new MinhaModel();
         var pessoaFisica = db.PessoasFisicas.Find(PessoaFisicaId);
         return pessoaFisica;
      }
   }
   ...
}

Personal Class:

public class PessoaFisica : Pessoa
{
   public string Cpf { get; set; }
   public virtual ApplicationUser Usuario
   {
      get
      {
         ApplicationDbContext db = new ApplicationDbContext();
         var usuario = db.Users.FirstOrDefault(u => u.PessoaFisicaId == Id);
         return usuario;
      }
   }
}

Use in a Controller:

public ActionResult Details(int id)
{
   PessoaFisica pessoaFisica = db.PessoasFisicas.Find(id);
   var nomeUsuario = pessoaFisica.Usuario.UserName;
   return View(pessoaFisica);
}

Use in a View:

@using Microsoft.AspNet.Identity
@using NomeAplicacao.Models
@using Microsoft.AspNet.Identity.EntityFramework
@{
    var manager = new UserManager(new UserStore(new ApplicationDbContext()));
    var usuario = manager.FindById(User.Identity.GetUserId());
}
…
@Html.ActionLink("Olá " + usuario.PessoaFisica.Nome + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
…

Browser other questions tagged

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