Associate IdentityUser to an Entity in EF Core

asp.net-core c# entity-framework-core

Question

Below is the IdentityUser subclass, and a simple entity. How can the IdentityUser be referenced within the Entity class? This application is using Entity Framework Core.

ApplicationUser.cs

using Microsoft.AspNetCore.Identity;

namespace MyProject.Models
{
    public class ApplicationUser : IdentityUser
    {

    }
}

Entity.cs

namespace MyProject.Models
{
    public class Entity
    {
        public int EntityId { get; set; }

        /* What belongs here to associate to a user? */
    }
}
1
2
6/30/2018 2:25:47 PM

Accepted Answer

The convention-over-configuration way would be:

public class Entity
{
    public int EntityId { get; set; }
    public string ApplicationUserId { get; set; }

    public ApplicationUser ApplicationUser { get; set; }
}

And the following is optional, you can omit it if you don't need to get all Entities for an user.

public class ApplicationUser : IdentityUser
{
    public ICollection<Entity> Entities { get; set; }
}
2
7/1/2018 1:23:15 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow