C# ASP.NET MVC延迟加载不使用ICollection
我已经用这两种模型创建了一个拍卖网站:C# ASP.NET MVC延迟加载不使用ICollection,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我已经用这两种模型创建了一个拍卖网站: public class ItemModel { [Key] public int ItemModelId { get; set; } [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] public DateTime DateCreatedUtc { get; set; } ... [ForeignKey("AcceptedBidModel")]
public class ItemModel
{
[Key]
public int ItemModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
...
[ForeignKey("AcceptedBidModel")]
public int? AcceptedBidModelId { get; set; }
public virtual BidModel AcceptedBidModel { get; set; }
public virtual ICollection<BidModel> ItemBids { get; set; }
}
public class BidModel
{
[Key]
public int BidModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
[Required]
[Range(1, Int32.MaxValue, ErrorMessage = "The bid must have a positive value")]
public int Bid { get; set; }
[ForeignKey("ItemModel")]
public int ItemModelId { get; set; }
public virtual ItemModel ItemModel { get; set; }
}
有人能解释这种行为吗?我通过删除中标(
AcceptedBidModel
)解决了这个问题,并在BidModel
中创建了一个bool,指示该投标是否为中标投标
以下是新型号:
public class ItemModel
{
[Key]
public int ItemModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
...
public virtual ICollection<BidModel> ItemBids { get; set; }
[NotMapped]
public BidModel AcceptedBid
{
get {
return ItemBids.Where(s => s.Accepted).SingleOrDefault();
}
set {}
}
}
public class BidModel
{
[Key]
public int BidModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
public bool Accepted { get; set; }
[Required]
[Range(1, Int32.MaxValue, ErrorMessage = "The bid must have a positive value")]
public int Bid { get; set; }
[ForeignKey("ItemModel")]
public int ItemModelId { get; set; }
public virtual ItemModel ItemModel { get; set; }
}
公共类ItemModel
{
[关键]
public int ItemModelId{get;set;}
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”)]
公共日期时间DateCreatedUtc{get;set;}
...
公共虚拟ICollection ItemBids{get;set;}
[未映射]
公开招标模式中标
{
得到{
返回ItemBids.Where(s=>s.Accepted).SingleOrDefault();
}
集合{}
}
}
公共类投标模型
{
[关键]
public int BidModelId{get;set;}
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”)]
公共日期时间DateCreatedUtc{get;set;}
公共布尔接受{get;set;}
[必需]
[范围(1,Int32.MaxValue,ErrorMessage=“投标必须有正值”)]
公共整数Bid{get;set;}
[外键(“项目模型”)]
public int ItemModelId{get;set;}
公共虚拟ItemModel ItemModel{get;set;}
}
您能告诉我您要添加的代码和未成功的代码吗?您的表是如何设计的?如果BidModel
和ItemModel
之间存在一个多关系,那么拥有ICollection
是有意义的,但是拥有另一个BidModel
属性将改变行为。一个ItemModel
可以拥有多个BidModel
,但是我想在ItemModel
中为中标人提供另一个BidModel
。这不可能吗?不知何故,当我添加额外的BidModel
时,惰性加载变得混乱起来。ItemModel
的构造函数中有什么东西吗?根本没有构造函数@GertArnold
public class ItemModel
{
[Key]
public int ItemModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
...
public virtual ICollection<BidModel> ItemBids { get; set; }
[NotMapped]
public BidModel AcceptedBid
{
get {
return ItemBids.Where(s => s.Accepted).SingleOrDefault();
}
set {}
}
}
public class BidModel
{
[Key]
public int BidModelId { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime DateCreatedUtc { get; set; }
public bool Accepted { get; set; }
[Required]
[Range(1, Int32.MaxValue, ErrorMessage = "The bid must have a positive value")]
public int Bid { get; set; }
[ForeignKey("ItemModel")]
public int ItemModelId { get; set; }
public virtual ItemModel ItemModel { get; set; }
}