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.