Search code examples
asp.net-core-mvcentity-framework-coreasp.net-core-identity

Identity User FK


I've an application in Asp.NET Core 3.1 MVC with EF Core and Identity.

I've two tables Calls and AspNetUsers. AspNetUsers has many Calls and one Call has one AspNetUsers.

The Calls table structure is okay, I think. But now I need to get Calls from AspNetUsers.

In CallsController I'm trying: IList<Call> calls = this.User.Calls; but no success.

I tried:

IList<Call> calls = this._context.Calls.Where(x => x.UserId == this._userManager.GetUserId(this.User)).ToList(); 

I've success. But do it is correct?

So, in application i've identity classes and an ApplicationUser like this:

public class ApplicationUser : IdentityUser
{
    public virtual IList<Call> Calls { get; set; }
}

And in Startup class in ConfigureServices method:

services.AddDefaultIdentity<ApplicationUser>(options => 
    options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

So, What's the better way to get Calls from AspNetUsers? Thanks!


Solution

  • You can set ApplicationUser like :

    public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<Call> Calls { get; set; }
    }
    

    Call.cs:

    public class Call
    {
        public int ID { get; set; }
    
        public string name { get; set; }
    
        // other properties
    
        public string UserID { get; set; }
        [ForeignKey("UserID")]
        public virtual ApplicationUser ApplicationUser { get; set; }
    }
    

    In ApplicationDbContext , add :

    public virtual DbSet<Call> Calls { get; set; } //add this line 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    

    Then you can query the current user's call by :

    if (User.Identity.IsAuthenticated)
    {
        var userID = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
        var calls = _applicationDbContext.Users.Include(u => u.Calls).First(u => u.Id == userID).Calls.ToList();
    
        //or
        var callsa = _applicationDbContext.Calls.Where(p => p.UserID == userID).ToList();
    }