0

I have two models:

ApplicationUsers: ( The default that comes with Identity framework )

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

and Room:

public class Room
{
    public int Id { get; set; }
    public string RoomName { get; set; }
    public ApplicationUser User1 { get; set; }
    public ApplicationUser User2 { get; set; }
    public ApplicationUser User3 { get; set; }
}

I created a new user at AppliactionUser:

ApplicationUsers table And I manually added a new Room and reference to that User:

Rooms Table

NOTE: It might not seem from the images, but their ids are the same, I doubled-checked it, the reason it looks different in these pictures is because there's not enough space to display the full length.

But when I ran my web and wrote this code on Home Controller :

public class HomeController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();
    public ActionResult Index()
    {
        var varForDebug = db.Rooms.Find(1); // Room id 1

        return View();
    }

and made a breaking point at varForDebug I couldn't see an ApplicationUser at User1:

enter image description here

Why is that and how it could be solved?

Note: I'm using: ASP.NET MVC Web Application (.NET Framework ) 4.6.1 with Identity.

Note2: Include wants only string parameter from some reason, whys that?

Maybe I should consider adding Room property to ApplicationUser?

Dorki
  • 1,021
  • 2
  • 8
  • 23
  • 2
    Either enable lazy loading and make those properties virtual or follow the below answer about calling Include for each relationship you need to load like `Include("propertyname")` – Mat J Aug 25 '19 at 16:29

2 Answers2

2

First, add the references:

using System.Linq;

and

using System.Data.Entity;

If you want to keep using the Find operation, use:

 var varForDebug = db.Rooms.Find(1); // Room id 1 `

 // Load the user 
 context.Entry(varForDebug).Reference(p => p.User1).Load();

or

db.Rooms.Include(r => r.User1).FirstOrDefault(x=>x.Id==1);
David Donari
  • 599
  • 7
  • 17
1

Use db.Rooms.Include(r => r.User1).FirstOrDefault(r => r.Id == 1);

and repeat that for User2, User3 etc.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    I tried, tho it says: "Cannot convert lambda expression to type 'String' because it is not delegate type". – Dorki Aug 25 '19 at 16:29
  • 2
    Yeah, Include() and Find() don't go together. I just took a gamble. Corrected. – H H Aug 25 '19 at 16:33
  • Oh, and I'm thinking EF Core. I forgot the details for EF6. – H H Aug 25 '19 at 16:34
  • It still insists to get String as a parameter in Include, it keeps saying: "Cannot convert lambda expression to type 'String' because it is not delegate type". I might need to add Room property to ApplicationUser? – Dorki Aug 25 '19 at 16:37
  • 1
    Try `Include("User1")` – H H Aug 25 '19 at 16:45
  • It works! thanks a lot, in order to repeat that for user 2 and 3, how it should look like? – Dorki Aug 25 '19 at 16:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198448/discussion-between-dorki-and-henk-holterman). – Dorki Aug 25 '19 at 16:47
  • 2
    You can have multiple `.Include("...")` – H H Aug 25 '19 at 16:51