C# 导航属性返回不完整的实体集
这里是我的实体的简化模型C# 导航属性返回不完整的实体集,c#,entity-framework-6,C#,Entity Framework 6,这里是我的实体的简化模型 public class Location { [Key] [StringLength(8)] public string Code { get; set; } [Required] [StringLength(100)] public string FriendlyName { get; set; } public virtual ICollection<Move> Moves { get
public class Location
{
[Key]
[StringLength(8)]
public string Code { get; set; }
[Required]
[StringLength(100)]
public string FriendlyName { get; set; }
public virtual ICollection<Move> Moves { get; set; }
}
public class Move
{
[Key]
public Guid Id { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[StringLength(8)]
public string LocationFromCode { get; set; }
[Required]
[StringLength(8)]
public string LocationToCode { get; set; }
[ForeignKey("LocationFromCode")]
public Location LocationFrom { get; set; }
[ForeignKey("LocationToCode")]
public Location LocationTo { get; set; }
}
公共类位置
{
[关键]
[第(8)款]
公共字符串代码{get;set;}
[必需]
[长度(100)]
公共字符串FriendlyName{get;set;}
公共虚拟ICollection移动{get;set;}
}
公开课运动
{
[关键]
公共Guid Id{get;set;}
[必需]
公共日期时间日期{get;set;}
[必需]
[第(8)款]
公共字符串位置FromCode{get;set;}
[必需]
[第(8)款]
公共字符串LocationToCode{get;set;}
[ForeignKey(“LocationFromCode”)]
公共位置LocationFrom{get;set;}
[外键(“LocationToCode”)]
公共位置LocationTo{get;set;}
}
为了避免周期性引用问题,我在上下文中实现了以下逻辑
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Location>()
.HasMany(l => l.Moves)
.WithRequired(m => t.LocationFrom)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Location>()
.HasMany(a => a.Moves)
.WithRequired(t => t.LocationTo)
.WillCascadeOnDelete(false);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(l=>l.Moves)
.WithRequired(m=>t.LocationFrom)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasMany(a=>a.Moves)
.WithRequired(t=>t.LocationTo)
.WillCascadeOnDelete(假);
}
我面临的问题是,我只能通过位置来查看。移动
仅限于使用LocationTo->Location约束的移动
这是EF限制还是我做错了什么?公共类位置
public class Location
{
[Key]
[StringLength(8)]
public string Code { get; set; }
[Required]
[StringLength(100)]
public string FriendlyName { get; set; }
public virtual ICollection<Move> MovesTo { get; set; }
public virtual ICollection<Move> MovesFrom { get; set; }
}
public class Move
{
[Key]
public Guid Id { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[StringLength(8)]
public string LocationFromCode { get; set; }
[Required]
[StringLength(8)]
public string LocationToCode { get; set; }
[ForeignKey("LocationFromCode")]
[InverseProperty("MovesFrom")]
public Location LocationFrom { get; set; }
[ForeignKey("LocationToCode")]
[InverseProperty("MovesTo")]
public Location LocationTo { get; set; }
}
to avoid a cyclical reference issue:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Location>()
.HasMany(l => l.MovesFrom)
.WithRequired(m => t.LocationFrom)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Location>()
.HasMany(a => a.MovesTo)
.WithRequired(t => t.LocationTo)
.WillCascadeOnDelete(false);
}
{
[关键]
[第(8)款]
公共字符串代码{get;set;}
[必需]
[长度(100)]
公共字符串FriendlyName{get;set;}
公共虚拟ICollection移动到{get;set;}
公共虚拟ICollection从{get;set;}移动
}
公开课运动
{
[关键]
公共Guid Id{get;set;}
[必需]
公共日期时间日期{get;set;}
[必需]
[第(8)款]
公共字符串位置FromCode{get;set;}
[必需]
[第(8)款]
公共字符串LocationToCode{get;set;}
[ForeignKey(“LocationFromCode”)]
[反向属性(“移动自”)]
公共位置LocationFrom{get;set;}
[外键(“LocationToCode”)]
[反向属性(“移动到”)]
公共位置LocationTo{get;set;}
}
为避免周期性参考问题:
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasMany(l=>l.MovesFrom)
.WithRequired(m=>t.LocationFrom)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasMany(a=>a.MovesTo)
.WithRequired(t=>t.LocationTo)
.WillCascadeOnDelete(假);
}
您可以查看@MBakardzhiev,谢谢!似乎无法定义累积导航属性。贝娄:我已经发布了我的研究结果,这是一个答案?是的,这是一个经过验证的版本。将对答案添加一些注释