views:

474

answers:

2

Hi,

I'm trying to retrieve a user object from a foreign key reference but each time I try to do so nothing gets returned...

My table is set up like this:

  FBUserID long,
  UserID uniqueidentifier

so I have my repository try to get the User when it's provided the FBUserID:

    public User getUserByFBuid(long uid)
    {
        User fbUser = null;
        IEnumerable<FBuid> fbUids = _myEntitiesDB.FBuidSet.Where(user => user.FBUserID == uid);

        fbUser = fbUids.FirstOrDefault().aspnet_Users;

        return fbUser;
    }

I've checked that the uid (FBUserID) passed in is correct, I've check that the UserID is matched up to the FBUserID. And I've also checked to make sure that fbUids.Count() > 0...

I've returned fbUids.FirstOrDefault().FBUserID and gotten the correct FBUserID, but any time I try to return the aspnet_Users or aspnet_Users.UserName, etc... I don't get anything returned. (I'm guessing it's getting an error for some reason)

I don't have debugging set up properly so that's probably why i'm having so much troubles... but so far all the checking I've done I've been doing return this.Json(/* stuff returned form the repository */) so that I can do an alert when it gets back to the javascript.

Anyone know why I would have troubles retrieving the user object from a foreign key relationship like that?

Or do you have any suggestions as to finding out what's wrong?

Thanks,
Matt

A: 

Don't know if this is what you are asking but i do a join like so;

var votes = from av in dc.ArticleVotes
  join v in dc.Votes on av.voteId equals v.id
  where av.articleId == articleId
  select v;

Did this help or am I off base?

griegs
I kind of understand what you're doing... but if I do user.aspnet_Users shouldn't that do all the sql execution for me to retrieve that user? (If aspnet_Users is a foreign key). Could you explain a bit more?
Matt
The [only] explanation I can give you is that I do the above to get around exactly the same issue you are having. You can use something like this._Items = new EntitySet<Item>(new Action<Item>(this.attach_Items), new Action<Item>(this.detach_Items)); and then all the other stuff but I never really got this to work and needed to move on. If you have a LinqToSql.dbml then check for the above and you'll see what I mean.
griegs
It is almost never correct to use join in LINQ to Entities. You almost always use relationships instead.
Craig Stuntz
A: 

For now, with Entity Framework 1, you don't get automatic delayed loading, e.g. if you want to traverse from one entity to the next, you need to either do an .Include("OtherEntity") on your select to include those entities in the query, or you need to explicitly call .Load("OtherEntity") on your EntityContext to load that entity.

This was a design decision by the EF team not to support automagic deferred loading, since they considered it to be too dangerous; they wanted to make it clear and obvious to the user that he is also including / loading a second set of entities.

Due to high popular demand, the upcoming EF v4 (to be released with .NET 4.0 sometime towards the end of 2009) will support the automatic delayed loading - if you wish to use it. You need to explicitly enable it since it's off by default:

context.ContextOptions.DeferredLoadingEnabled = true;

See some articles on that new feature:

marc_s