In Developing Code First how do I relate the AspNetUsers table to other table

1

I'm developing a real-estate system and need to match the AspNetUsers table with the tables that the system will have and cardinality one to many , so I want the system send me reports of who performed registration features and other functions performed day by day in the system.

If someone has a better control type you can indicate me, thank you very much, but still pass the solution to 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 is the owner of the 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; }
}
    
asked by anonymous 18.12.2017 / 01:46

2 answers

0

You can not understand much what you need, a 1 / M relation is made with

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

in the table that has the relationship 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; }
}
    
18.12.2017 / 12:58
0

How to relate your entities has already been answered, but there is another way to do this without relating them: When your user logs in you create a UserList class with the aspNetUserId property and write to an authCookie. So every time you need the User Id you read the cookie and fill in the UserList object. It's another way to do this, 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 do I read the cookie? The least repetitive way is to create a controller to do this 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 on your controller, you make the controller inherit from the baseController:

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

  // aqui você pode chamar: UsuarioLogado.AspNetUserId 

NOTE: You want to report, so you lose the relationship and use (virtual) browsing properties but you can easily query and bring in 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));
    
18.12.2017 / 14:46