实体框架包括命令 - 向左或内部联接? [英] Entity framework Include command - Left or inner join?
问题描述
当我正在调查的的区别包括:
和加入
我发现:
As I was investigating the difference between Include
and Join
I found that :
如果数据库的不的包括外键 - 它没有导航道具,因此最好使用加入
If the DB does not include a Foreign Keys -it has no navigation props so it's better to use Join
如果它的确实的具有导航道具 - 然后用包含
。 (这也节省了分贝命中。)
If It does have a navigation props - then use Include
. ( it also save a db hit.)
但有 回答这里 引起了我的注意:
But one answer here caught my attention:
包括被实现为一个连接。 根据空性的 包含的链接是一个内或离开加入。
Include is implemented as a join. Depending on the nullability of the included link it is an inner or left join.
问:
如何在法律上无效影响了左/内部联接?
How does the nullity affects the left / inner join ?
在SQL Server中,我可以有一个城市
表和人
表,一个人可以有一个 NULL
CityID
。
In Sql server I can have a Cities
table and Persons
table and a person can have a NULL
CityID
.
为什么做实体框架的决定对我来说是什么样的加入?
Why does entity Framework decides for me what kind of join it is ?
现在,让我们改变CityId不空:
Now lets change CityId to not null :
和这里的变化:
推荐答案
假设在你的类存在于城市或CityID一个 [必需]
的约束。假设有记录的人没有(有效的)城市。只有这样,才能满足 [必需]
是执行内连接。
Suppose that in your class there is a [Required]
constraint on City or CityID. And suppose there are Person records without a (valid) City. The only way to satisfy the [Required]
is to perform an inner join.
不过,只要你的数据库和模型匹配的约束(即 CityID INT NOT NULL
),它不会真的不管什么样的加入使用。这应该是正常的情况。
But as long as the constraints in your Db and model match (ie CityID INT NOT NULL
) it wouldn't really matter what kind of Join is used. That should be the normal case.
和没有约束,你当然会想到左连接。
And without the constraint you would of course expect a Left Join.
这篇关于实体框架包括命令 - 向左或内部联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!