开发者

How to get data from relationship objects in entity framework?

I have 2 Entity objects that i mapped in Entityframework

I have relationship between users and Messages I开发者_JS百科 want to get all messages of specific user so i load the data from users table to specific user object:

var user = (from u in context.Users
            where u.u_username == username
            select u).First();

Now, how can i get all messages of this user , i notice that i can do:

var messages = user.Messages; 

But i got nothing. Is it correct?

How can i used this syntax and to get all message of the specific user?

I used .net4 Is it possible to do


If you want to lazy load the Messages of the user you can make the Messages property virtual.

public class User
{
    //other properties
    public virtual ICollection<Message> Messages;
}

Or you can eager load Messages using Include function

var user = (from u in context.Users.Include("Messages")
            where u.u_username == username
            select u).First();

Finally it maybe the case that you have not configured the Messages property with EF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜