Associate ApplicationUser of Identity with my class PersonPhysical

0

My Asp.Net MVC system uses Identity in its default form, with some simple customizations. I also have a Physical Persons table (which inherits some information from People, but I think this is not the case).

I would like to associate the ApplicationUser with the PersonPass, so that every ApplicationUser has a PersonPass (PersonPass 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 PersonPass 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 error I get is:

One or more validation errors were detected during model generation:

ApplicationUser_Pessoa_Source:: Multiplicity is not valid in Role 'ApplicationUser_PessoaFisica_Source' in relationship 'ApplicationUser_PessoaPessoa'. 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 also have one error, but it's another one:

Unable to determine the principal 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 fluent relationship API or data annotations.

What am I doing wrong?

    
asked by anonymous 01.02.2018 / 12:44

1 answer

0

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

I ended up doing a half-way relationship by consulting my table every time I want to consult the user of a physical person. The same I do to consult the physical person of a user. Hope it helps 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;
      }
   }
   ...
}

ClassPhysical 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" })
…
    
19.02.2018 / 15:10