I am working with .NET Core 2.2 and I am having trouble understanding the right way to save data owned by a user.
I am using the out of the box identity scaffolding and code first migrations.
Users are stored in the AspNetUsers
table and I'm leveraging Google/Facebook providers.
As a basic example, if I have a simple model such as this, I can't find a clean example of how to reference the owner of the data in the model.
public class UserFavoriteColor
{
public int Id { get; set; }
public string Color {get; set;
public IdentityUser User { get; set; } // I've seen this in some documentation
public string UserId { get; set; } // I've seen this as well
// Other ways??
}
Additionally, there seems to be a handful of ways to retrieve and store the user information as well:
// one way to get the id
User.FindFirst(ClaimTypes.NameIdentifier).Value;
// another way
_userManager = MockUserManager.GetUserManager<ApplicationUser>();
Maybe I'm completely missing something obvious here, but I would love to see a very basic model and controller example that saves a record with the users identity value as a foreign key with a best practice approach.
Thanks!