In Development Code First how do I relate the Aspnetusers table to other tables

Asked

Viewed 190 times

1

I am developing a system for real estate and need to relate the Table Aspnetusers with the tables that the system will have and with cardinality one for many, this I want so that the system can pass me reports of who made registration functionalities and among other functionalities executed day by day in the system.

If someone has a type of control better can indicate me, I really appreciate, but even so pass the solution to this my doubt, which may not be one of the best and can solve in parts.

One of the tables I want to have control of which user made the registration is the Owner Table.

public class Proprietario
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Endereco { get; set; }
    public string Numero { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }
    public string Cep { get; set; }
    public string Rg { get; set; }
    public string Cpf { get; set; }
    public string Telefone { get; set; }
    public string Celular { get; set; }
    public DateTime DataCadastro { get; set; }
}

2 answers

0

I didn’t really understand what you need, a relationship 1/M is made with the ;

public virtual ICollection<SuaClasse> SuaClasse{ get; set; }

in the table that has the relation 1/M

I believe it would be in your case.

publuc class AspNetUsers
{
   // ...
   public virtual ICollection<Proprietario> Proprietario{ get; set; }
}

public class Proprietario
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Endereco { get; set; }
    public string Numero { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }
    public string Cep { get; set; }
    public string Rg { get; set; }
    public string Cpf { get; set; }
    public string Telefone { get; set; }
    public string Celular { get; set; }
    public DateTime DataCadastro { get; set; }
}
  • is the following, when we create an Asp.Net MVC application we have the Change Authentication option, with the Individual User Account option selected, with this the application gets the user login functionality, then what I want, and realize the relationship of the logged in user with the Owner table, because the user will be able to register Multiple Owners, Understood ?

0

How to relate their entities has already been answered, but there is another way to do this without relating them: When your user logs in you create a User class with the aspNetUserId property and save to an authCookie. Then each time you need the User Id you read the cookie and fill in the User object. It’s another way to do it, I’m not saying it’s the best...

In your Login:

        if (ModelState.IsValid)
        {

            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

            if (result.Succeeded)
            {
                var usuario = await _userManager.FindByEmailAsync(model.Email);

                var usuarioLogado = new UsuarioLogado
                {
                    AspNetUserId = usuario.Id,
                    // outros dados que quiser
                };

                //cria cookie de autenticação
                _httpContext.SetAuthCookie(_dataProtectionProvider, model.RememberMe, usuarioLogado);

But then how to read the cookie? The least repetitive way is to create a controller to do it for you:

public class BaseController : Controller
{
    private readonly IDataProtectionProvider _dataProtectionProvider;

    public BaseController(IDataProtectionProvider dataProtectionProvider)
    {
        _dataProtectionProvider = dataProtectionProvider;
    }

    public UsuarioLogado UsuarioLogado { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        try
        {
            base.OnActionExecuting(filterContext);
        }
        catch (System.Exception e)
        {

            throw;
        }

        var httpContext = Request.HttpContext;

        try {
            var usuario = httpContext.GetAuthCookieData<UsuarioLogado>(_dataProtectionProvider);

            UsuarioLogado = usuario;
        }
        catch (System.Exception e) {
            httpContext.Response.Redirect("~/Account/?erro=1");
        }
    }
}

And then when you need to use the logged in user in your controller, you make the controller inherit from baseController:

[Route("api/Teste")]
public class ApiTesteController : BaseController
{

  // aqui você pode chamar: UsuarioLogado.AspNetUserId 

NOTE: You want to make reports, this way you lose the relationship and the use of browsing properties (virtual) but you can easily make the query and bring the data you want. If you are using entityFramework:

 var proprietarios = (context ou repositorio de proprietario).Where(x => x.UsuarioId == (usuario logado ou qualquer outro));

Browser other questions tagged

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