我有像这样的域名模型
public class ShipToCode
{
public int ShipToCodeID { get; set; }
[Required(ErrorMessage = "Nombre es requerido",AllowEmptyStrings = false)]
[StringLength(50, ErrorMessage = "Debe ser menor a 50 carácteres")]
[MaxLength(50)]
[Column("nb_name")]
public string Name { get; set; }
[StringLength(15, ErrorMessage = "Debe ser menor a 15 carácteres")]
[MaxLength(15)]
[Column("cd_locId")]
public string locId { get; set; }
public int? RegionID { get; set; }
public virtual Region Region { get; set; }
[Required(ErrorMessage = "Dealer es requerido")]
public int DealerID { get; set; }
public virtual Dealer Dealer { get; set; }
}
public class Dealer
{
public Dealer()
{
this.ShipToCode = new HashSet<ShipToCode>();
}
public int DealerID { get; set; }
[Required(ErrorMessage = "Nombre es requerido")]
[StringLength(100, ErrorMessage = "Debe ser menor a 100 carácteres")]
[MaxLength(100)]
[Column("nb_name")]
public string Name { get; set; }
[Required(ErrorMessage = "Codigo de distribuidor es requerido")]
[StringLength(100, ErrorMessage = "Debe ser menor a 100 carácteres")]
[MaxLength(100)]
[Column("cd_dealerCode")]
public string DealerCode { get; set; }
public virtual ICollection<ShipToCode> ShipToCode { get; private set; }
}
在我的背景下
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ShipToCode>()
.HasRequired<Dealer>(d => d.Dealer)
.WithMany(d => d.ShipToCode)
.HasForeignKey<int>(c => c.DealerID);
}
但是当我得到任何ShipToCode或经销商时,使用DbSet的find功能,我在ShipToCode中的经销商对象或经销商的ShipToCode的ICollection是空的
这是正常的还是我错过了什么?请帮助!!
答案 0 :(得分:0)
在您的经销商处,物业,
public virtual ICollection<ShipToCode> ShipToCode { get; private set; }
您需要使用private
set方法将其公开,否则EF无法从数据库中设置它。并确保您已在context
中启用了延迟加载功能。如果你没有启用延迟加载,你可以使用这样的热切加载,
Context.ShipToCodes.Include(s=>s.Dealer ).where(s=>s.ShipToCodeID ==1)