Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 是否先在代码中包含外键Id以保存查询?_Database_Entity Framework_Foreign Keys_Entity Framework 6 - Fatal编程技术网

Database 是否先在代码中包含外键Id以保存查询?

Database 是否先在代码中包含外键Id以保存查询?,database,entity-framework,foreign-keys,entity-framework-6,Database,Entity Framework,Foreign Keys,Entity Framework 6,我首先使用实体框架6代码 现在,我的模型如下所示: public class Region { public int Id { get; set; } public string Name { get; set; } public virtual List<City> Cities { get; set; } } public class City { public int Id { get; set; } public string Nam

我首先使用实体框架6代码

现在,我的模型如下所示:

public class Region
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<City> Cities { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Required]
    public virtual Region Region { get; set; }
}
    foreach (var c in cities)
    {
        if (regions.Any(x => x.Id == c.Region.Id))
    }
public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("Region")]
    public int RegionId { get; set; }
    [Required]
    public virtual Region Region { get; set; }
}
在这里,我必须从数据库中查找每个城市的一个区域(延迟加载)。 但是,我只需要区域的Id,因此在每个循环中查找区域行对我来说似乎是浪费

如果我将我的城市模型更改为以下:

public class Region
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<City> Cities { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    [Required]
    public virtual Region Region { get; set; }
}
    foreach (var c in cities)
    {
        if (regions.Any(x => x.Id == c.Region.Id))
    }
public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("Region")]
    public int RegionId { get; set; }
    [Required]
    public virtual Region Region { get; set; }
}
我可以这样做:

foreach (var c in cities)
    {
        if (regions.Any(x => x.Id == c.RegionId)) //no region lookup at Im using the foreign id key
    }
这是正确的吗?我的意思是,这将为我节省对每个城市的查询,对吗

如果是这样的话,那么在开始编写代码时,是否有任何理由不在模型中包含外来id键

这是正确的吗?我的意思是,这将为我节省对每个城市的查询,对吗

它将保存到区域表的联接。您必须使用SQL探查器检查它!例如:

Id为City1、City2的城市列表(未加载城市1的区域=区域Id为5) 现在,您正在查找Id为5的任何城市的区域

// EF does not have to join the tables because you have the RegionId
if (myDbContext.Cities.Any(c => c.RegionId == 5))
{
} 
如果是,是否有任何理由不将外来id密钥包括在 首先执行代码时的模型

不,对我来说,这是一个很好的练习!只要保持一致,并对类型为1..n/1..0或1的所有关系执行此操作