Entityframeworkcore Include 方法不起作用。急切加载延迟加载无法正常工作
Entityframeworkcore Include method doesnt working. Eager Loading Lazy Loading doesn't working correctly
Relationship tables are not added to stations(Lists) when I use the Include () method. eager loading and lazy loading both do not work.the associated station list is always zero.I'm using aspnet core mvc 5.0. any library missing? It pulls data from the database but does not fetch any associated data.
[HttpGet]
public IActionResult Index(int? SayfaNo)
{
int _sayfaNo = SayfaNo ?? 1;
// Proplem is here. Networks.Stations.Count=0 always
var networks = _context.Networks.Include(x=>x.Stations).ToList();
//.ToPagedList<Network>(_sayfaNo, 9);
if (networks == null)
{
return View(networks);
}
var request = Request;
if (request.Headers!=null)
{
if (request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return PartialView("~/Views/Home/_BicycleListPartialView.cshtml", networks);
}
}
return View(networks);
}
public class Network:BaseEntity
{
public Network()
{
Stations = new HashSet<Station>();
}
public string NId { get; set; }
public string Name { get; set; }
[ForeignKey("Location")]
public int LocationId { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<Station> Stations { get; set; }
}
public class Location:BaseEntity
{
public string Country { get; set; }
public string City { get; set; }
public virtual Network Network { get; set; }
}
public class Station:BaseEntity
{
public string SId { get; set; }
public string Name { get; set; }
public int? FreeBikes { get; set; }
public int? EmptySlots { get; set; }
[ForeignKey("Network")]
public int NetworkId { get; set; }
public virtual Network Network { get; set; }
}
public class BaseEntity
{
[Key]
public int Id { get; set; }
}
我看到几个问题:
[ForeignKey]
属性可以放在导航 属性 或 FK 字段上,但是属性必须指向 other 属性. IE。如果你把它放在 FK 字段上,它应该指向导航 属性:
[ForeignKey("Network")]
public int NetworkId { get; set; }
public virtual Network Network { get; set; }
.. 或者这个:
public int NetworkId { get; set; }
[ForeignKey("NetworkId")]
public virtual Network Network { get; set; }
在 Location 和 Network 之间声明的一对一关系似乎也存在冲突,其中 Network 应具有单个 Location,而 Location 具有单个 Network。默认情况下,一对一关系期望连接两个表的主键。如果这是多对一关系(网络有一个位置,位置可以是许多网络的一部分,但可以有一个“默认”网络),那么位置将需要网络的 FK。 (即 DefaultNetworkId)以及网络和位置之间的关系需要明确配置。如果取而代之的是从位置到网络的多对一关系,则情况相同。相反,如果它是一对一关系,那么我相信 EF Core 可以配置 1 对 1/w 配对 FK,否则它会期望它们的 ID 匹配。无论哪种方式,它都可能需要对关系进行一些显式配置。
接下来我要避免在构造函数中初始化引用导航属性。初始化 child 对象或集合是可以的,但要避免初始化引用:
public Network()
{
// Location = new Location(); <- don't initialize as a *new* one is not a valid state.
Stations = new HashSet<Station>();
}
Relationship tables are not added to stations(Lists) when I use the Include () method. eager loading and lazy loading both do not work.the associated station list is always zero.I'm using aspnet core mvc 5.0. any library missing? It pulls data from the database but does not fetch any associated data.
[HttpGet]
public IActionResult Index(int? SayfaNo)
{
int _sayfaNo = SayfaNo ?? 1;
// Proplem is here. Networks.Stations.Count=0 always
var networks = _context.Networks.Include(x=>x.Stations).ToList();
//.ToPagedList<Network>(_sayfaNo, 9);
if (networks == null)
{
return View(networks);
}
var request = Request;
if (request.Headers!=null)
{
if (request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return PartialView("~/Views/Home/_BicycleListPartialView.cshtml", networks);
}
}
return View(networks);
}
public class Network:BaseEntity
{
public Network()
{
Stations = new HashSet<Station>();
}
public string NId { get; set; }
public string Name { get; set; }
[ForeignKey("Location")]
public int LocationId { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<Station> Stations { get; set; }
}
public class Location:BaseEntity
{
public string Country { get; set; }
public string City { get; set; }
public virtual Network Network { get; set; }
}
public class Station:BaseEntity
{
public string SId { get; set; }
public string Name { get; set; }
public int? FreeBikes { get; set; }
public int? EmptySlots { get; set; }
[ForeignKey("Network")]
public int NetworkId { get; set; }
public virtual Network Network { get; set; }
}
public class BaseEntity
{
[Key]
public int Id { get; set; }
}
我看到几个问题:
[ForeignKey]
属性可以放在导航 属性 或 FK 字段上,但是属性必须指向 other 属性. IE。如果你把它放在 FK 字段上,它应该指向导航 属性:
[ForeignKey("Network")]
public int NetworkId { get; set; }
public virtual Network Network { get; set; }
.. 或者这个:
public int NetworkId { get; set; }
[ForeignKey("NetworkId")]
public virtual Network Network { get; set; }
在 Location 和 Network 之间声明的一对一关系似乎也存在冲突,其中 Network 应具有单个 Location,而 Location 具有单个 Network。默认情况下,一对一关系期望连接两个表的主键。如果这是多对一关系(网络有一个位置,位置可以是许多网络的一部分,但可以有一个“默认”网络),那么位置将需要网络的 FK。 (即 DefaultNetworkId)以及网络和位置之间的关系需要明确配置。如果取而代之的是从位置到网络的多对一关系,则情况相同。相反,如果它是一对一关系,那么我相信 EF Core 可以配置 1 对 1/w 配对 FK,否则它会期望它们的 ID 匹配。无论哪种方式,它都可能需要对关系进行一些显式配置。
接下来我要避免在构造函数中初始化引用导航属性。初始化 child 对象或集合是可以的,但要避免初始化引用:
public Network()
{
// Location = new Location(); <- don't initialize as a *new* one is not a valid state.
Stations = new HashSet<Station>();
}