如何.INCLUDE()的工作? [英] How does .Include() Work?

查看:121
本文介绍了如何.INCLUDE()的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在那里我添加导航属性使用实体图在Visual Studio中创建的数据库。我创建它使用本教程<一个href=\"http://geekswithblogs.net/danemorgridge/archive/2009/10/29/using-visual-studio-2010-beta-2-to-do-entity-framework.aspx\" rel=\"nofollow\">http://geekswithblogs.net/danemorgridge/archive/2009/10/29/using-visual-studio-2010-beta-2-to-do-entity-framework.aspx
它看起来像这样

Hi I created a database using an Entities diagram in visual studio where i added navigation properties. I created it using this tutorial http://geekswithblogs.net/danemorgridge/archive/2009/10/29/using-visual-studio-2010-beta-2-to-do-entity-framework.aspx It looks like this

User Table
IdUser    Name    IdAccount
1         Ron      2
2         Carl     1

导航属性:帐户(以一对一的关系)

Navigation property: Account (one to one relation)

Account table
IdAccount    Amount
1             50
2             30

导航属性:帐户(以一对一的关系
请注意,user1的帐户2和user2的帐户1

Navigation property: Account (one to one relation Notice that user1 has account 2 and user2 has account 1

我做了: VAR用户= infoDB.Users.Include(账户);

This displays:
Ron $50
Carl $30

这表明罗恩具有$ 50,它是帐户1的量,这是错误的,因为罗恩有帐号2 ....

It shows that Ron has $50 which is the amount of account1, which is wrong because Ron has account number 2....

为什么不是这方面的工作?

Why isn't this working?

我在几个表我也碰到过这种情况,不一定是ID用户必须是IdAccount,这仅仅是一个例子来说明这个问题。

I have come across with this in several tables I have, not necessarily the idUser has to be the IdAccount, this is just an example to illustrate the problem..

任何想法?我还有一直对这个我从头做了数据库和IdAccount是Account表的外键,但是它做同样的事情...

Any ideas? I have still been working on this I did the database from scratch and IdAccount IS the foreign key of Account table, but it is doing the same thing...

我在ASP MVC框架就好像从音乐教程中的查询这样做,但它似乎是工作的罚款与Genres.Include(相册),它有一个事实,即它是一个做一到一对一的关系?

I'm doing this in an ASP mvc framework it's like the query from the music tutorial, but there it seems to be working fine with Genres.Include("Albums") does it have to do with the fact that it's a one-to-one relationship?

推荐答案

从的这里

code首先现在推断任何财产名为&LT;导航属性名称&gt;&lt;主要关键属性名称&gt; (即SubjectISBN)&LT;主类名称&gt;&lt;主要关键属性名称&gt; (即BookISBN)或&LT ;主键属性名称&gt; (即ISBN),具有相同的数据类型作为主键,再presents的关系的外键。如果找到多个匹配,那么precedence在上面所列的顺序给出。外键检测将不区分大小写。

Code First will now infer that any property named <navigation property name><primary key property name> (i.e. SubjectISBN), <principal class name><primary key property name> (i.e. BookISBN) or <primary key property name> (i.e. ISBN), with the same data type as the primary key, represents a foreign key for the relationship. If multiple matches are found then precedence is given in the order listed above. Foreign key detection will not be case sensitive.

尝试围绕ACCOUNTID而不是IdAccount把你的键名。我的猜测是,它使用ID用户作为用户℃的外键; - >帐户关系

Try turning your key names around "AccountId" instead of "IdAccount". My guess is that it's using IdUser as a foreign key for the User<->Account relationship.

这篇关于如何.INCLUDE()的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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