Ado.Net实体:对象不显示链接的成员(外键) [英] Ado.Net Entity : Object doesn't display linked members (foreign keys)

查看:99
本文介绍了Ado.Net实体:对象不显示链接的成员(外键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单databasescheme:用户帐户。用户有1对多的与客户的关系。

I have a simple databasescheme: User, Account. User has 1-to-many relationship with Account.

我已经产生了ado.net实体数据模型,我可以创建用户和帐户,甚至将它们链接在一起。在数据库中的account.user_id正确填写,所以理论上我应该能够通过实体存取权限User.Account.ToList()在C#。

I have generated a ado.net entity data model, and I can create users and accounts, and even link them together. In the database the account.user_id is correctly filled, so theoretically I should be able to acces User.Account.ToList() in C# through entity.

然而,当我尝试存取权限User.Account.ToList()我得到结果为零。

However, When I try to acces User.Account.ToList() I get zero results.

User user = db.User.First(U => U.id == 1);
List<Account> accounts = user.Account.ToList(); ##count = 0...

在我的previous $ C $之前添加下面的code C,突然给了我正确的计数2。

When I add the following code before the previous code it suddenly gives me the correct count 2.

 Account account1 = db.Account.First(A => A.id == 1);
 Account account2 = db.Account.First(A => A.id == 2);
 User user = db.User.First(U => U.id == 1);
 List<Account> accounts = user.Account.ToList(); ##count = 2...??

我是什么在这里缺少??

What am I missing here??

推荐答案

您应该使用的 ObjectQuery.Include 方法这一点。你的方法有效,但也导致额外的查询。

You should use the ObjectQuery.Include method for this. Your method works also but results in an additional query.

在你的榜样,你会得到

User user = db.User.Include("Account").First(u => u.id == 1);

您必须找出字符串帐户是否正确。通常它应该是pfixed的东西,如 MyEntities $ P $。这取决于你的实体的命名空间,但有一点试验和错误,你应该能想出解决办法。

You have to figure out whether the string "Account" is correct. Usually it should be prefixed with something like MyEntities. This depends on the namespace of your entities but with a little trial and error you should be able to figure this out.

这篇关于Ado.Net实体:对象不显示链接的成员(外键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆