Simsons
Simsons

Reputation: 12735

Why custom properties for IdentityUser custom class are always null

I have a UserManager for my custom Identity user:

private readonly UserManager<CustomUser> _userManager;

Which has a collection of Subscriptions,

public class CustomUser:IdentityUser
{
    public ICollection<Subscription> Subscriptions { get; set; }
}

But when I retrieve the user using userManager:

  var user = await _userManager.FindByEmailAsync(model.UserName);

Subscriptions is null.

I am adding subscriptions and updating using:

 await _userManager.UpdateAsync(user);

When I check in database for Subscriptions, I could see the entries.

But user manager's Subscriptions is always returns null.

Upvotes: 2

Views: 246

Answers (1)

Simsons
Simsons

Reputation: 12735

UserManager.FindAsync.. methods does not include/return navigation properties.

To include navigation proporties we have to use the entityFramework extensions.

In this case use,

var userWithSubscriptions =  _userManager.Users.Include(c => c.Subscriptions);
var currentUserSubscription = userWithSubscriptions.Where(x => x.UserName == model.UserName).FirstOrDefault().Subscriptions;

Upvotes: 1

Related Questions