简体   繁体   中英

How to load linked entities with IdentityUser and Entity Framework

I'm currently writing a small test application to understand how IdentityUser works.

I've created a MyUser class that inherits from IdentityUser. The only additional property on my custom user class is a collection of my Book class.

I've created methods on the controller that successfully store new users to the database and associated Books. The problem is when I try to retrieve a user, the Books collection for that user is not populated - it's always null.

When I check the database I can see that a Book is stored in the database with an associated User ID however I can't seem to retrieve this collection.

Here is what I have so far:

Book.cs:

public class Book
{
    public int Id { get; set; }
    public string Isbn { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
}

MyUser.cs:

public class MyUser : IdentityUser
{
    public IList<Book> Books { get; set; }  
}

MyAppContext.cs:

public class MyAppContext : IdentityDbContext<MyUser>
{
    public MyAppContext() : base("MyApp")
    {

    }

    public DbSet<Book> Books { get; set; }
}

AuthRepository:

public class AuthRepository : IDisposable
{
    private MyAppContext _ctx;

    private UserManager<MyUser> _userManager;

    public AuthRepository()
    {
        _ctx = new MyAppContext();
        _userManager = new UserManager<MyUser>(new UserStore<MyUser>(_ctx));
    }

    public async Task<IdentityResult> RegisterUser(RegistrationModel userModel)
    {
        MyUser user = new MyUser();
        user.UserName = userModel.UserName;

        var result = await _userManager.CreateAsync(user, userModel.Password);

        return result;
    }

    public async Task<IdentityResult> UpdateUser(MyUser userModel)
    {
        var result = await _userManager.UpdateAsync(userModel);

        return result;
    }

    public async Task<MyUser> FindUser(string userName, string password)
    {
        var user = await _userManager.FindAsync(userName, password);

        return user;
    }

    public async Task<MyUser> GetUser(string userName)
    {
        var user = await _userManager.FindByNameAsync(userName);

        return user;
    }

    public void Dispose()
    {
        _ctx.Dispose();
        _userManager.Dispose();
    }
}

I figured maybe within the GetUser() method I could manually retrieve all books from the Book table with _ctx.Books.Where(b => b.MyUser_id == user.Id) however intellisense isn't even giving me the MyUser_Id property on the Books table.

I'm not really sure how to go about this. All I want to do is load all the associated books for a user automatically but I'm not sure how to do this. Any ideas?

Thanks

Your class for Book doesn't include user information for the foreign key reference. Try adding

[ForeignKey("UserId")]
public MyUser User { get; set; }

to the Book class definition.

When you get the users with the query

_ctx.Users.Include(u=> u.Books)

The books for each user should be included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM