Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# 相关ICollection实体不能为空_C#_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 相关ICollection实体不能为空

C# 相关ICollection实体不能为空,c#,.net,asp.net-mvc,asp.net-core,C#,.net,Asp.net Mvc,Asp.net Core,我的模型有以下字段: public ICollection<Guest> SecondaryGuests { get; set; } 这非常有效,除非我使用Create和Edit方法抛出异常: SecondaryGuests不能为空 我通过添加一个include,在Edit方法中绕过了这个问题: var guestToUpdate = await _context.Guests .Include(g => g.SecondaryGuests) .SingleO

我的模型有以下字段:

public ICollection<Guest> SecondaryGuests { get; set; }
这非常有效,除非我使用
Create
Edit
方法抛出异常:

SecondaryGuests不能为空

我通过添加一个
include
,在
Edit
方法中绕过了这个问题:

var guestToUpdate = await _context.Guests
    .Include(g => g.SecondaryGuests)
    .SingleOrDefaultAsync(g => g.ID == id);
然而,在
Create
方法中,我不知道该怎么做才能避免这个问题

我可以将
get
字段放在
ViewModel
中,但这会在我的索引和详细信息视图等上产生大量额外的工作。我宁愿修改
Create
方法,或者找出如何忽略
Create
方法上的
get

而不是使用
Any()
(当
SecondaryGuests
null
时会出现错误),只需检查
null
Count()


哎呀!这一次太复杂了,除了最基本的以外,尝试了各种各样的东西。
var guestToUpdate = await _context.Guests
    .Include(g => g.SecondaryGuests)
    .SingleOrDefaultAsync(g => g.ID == id);
return (GuestOf == null && SecondaryGuests != null && SecondaryGuests.Count() > 0 ?
        RSVP + SecondaryGuests.Count() : 0);