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));
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 ?
– Cyberlacs