Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
C# ASP.NET MVC延迟加载不使用ICollection_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

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; }
}