0

I have a situation like this:

Entity User
ICollection<UserPrincipal> UserPrincipals {get;set}

Entity UserPrincipal
UserId int (fk -> User.Id)
PrincipalId int (fk -> User.Id)

So the main object is User, there is a collection of UserPrincipal objects which just says which users are children of this user.

When I access the UserObject, UserPrincipals is populated correctly. UserPrincipal.UserId and UserPrincipal.PrincipalId get loaded correctly and UserPrincipal.User which was the user itself gets populated, but PrincipalUser is getting an object disposed error.

I get the original user like this:

        using (xxxServiceContext context = new xxxServiceContext())
        {
            retVal = context.xxxUsers.Include("UserProducts").Include("UserPrincipals").FirstOrDefault(o => o.UserName == userName && o.IsActive);
        }

So basically, what it seems like I need to do somehow is Include the other side of the M-M relationship in UserPrincipals... how do I get PrincipalUser to load the PrincipalId user?

EDIT: User looks like:

    public int Id { get; set; }
    public virtual ICollection<UserPrincipal> UserPrincipals { get; set; }

UserPrincipal looks like:

    public int Id { get; set; }
    public int UserId { get; set; }
    public int PrincipalId { get; set; }
    public virtual User User { get; set; }
    public virtual User PrincipalUser { get; set; }

In my test table, the UserPrincipals table has 1,2 for example.

So main User object is Id=1. UserPrincipals has 1 item. UserPrincipal.UserId=1, UserPrincipal.PrincipalUserId=2. UserPrincipal.User points back to user 1. So far so good. PrincipalUser has: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

I tried putting the ForeignKey attribute on UserPrincipal.User and UserPrincipal.PrincipalUser. Same thing.

Also tried changing UserPrincipal to:

    [InverseProperty("User")]
    public virtual ICollection<UserPrincipal> UserPrincipals { get; set; }
    [InverseProperty("PrincipalUser")]
    public virtual ICollection<UserPrincipal> UserPrincipals2 { get; set; }

UserPrincipals2 never got populated and still the object context disposed error.

My EF is ObjectContext, I don't have the OnModelCreating. Not really sure how I'm supposed to set up the fluent in this case.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • Basically, if you have 2 relationships from User to UserPrincipal you are going to need 2 collections in User (1 for each relationship). Then you would use either fluent code or annotations with InverseProperty so EF can straighten it out. Like [this](https://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table) – Steve Greene Apr 11 '18 at 00:38
  • @SteveGreene User has a collection of UserPrincipal's. A UserPrincipal has UserId / User and PrincipalId / Principal. UserId / User is going to be the user itself and PrincipalId / Principal is going to be the child user. User has multiple UserPrincipals, but each UserPrincipal is only indicating one child user. – SledgeHammer Apr 11 '18 at 00:52
  • @SteveGreene Seems like the ForeignKey thing won't work right? Because UserPrincipal is going to have User User; and User PrincipalUser; which somebody in the comments over there said EF won't know which way to map. InverseProperty looks interesting, but I think I have an older version of EF in this project. Not sure if that's available lol... I'll have to check. – SledgeHammer Apr 11 '18 at 00:54
  • Look at the link. User = Team, UserPrincipal = Match. – Steve Greene Apr 11 '18 at 01:35
  • @SteveGreene - Original post updated. Any suggestions? – SledgeHammer Apr 11 '18 at 04:20
  • Inverse version is correct. Have you thought about flipping your query? If you want a list of objects that points to the principal and user try: `var users = context.UserPrincipals.Include(up => up.User).Include(up => up.PrincipalUser).Where(up.User.UserName == userName && up.User.IsActive);` – Steve Greene Apr 11 '18 at 13:41

0 Answers0