Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# 未将实体添加到另一个实体集合_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 未将实体添加到另一个实体集合

C# 未将实体添加到另一个实体集合,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我有一个应用程序,用户可以创建一个事件,然后在创建事件后向该事件添加一张照片。这是事件实体: public class Event { public int Id { get; set; } public AppUser Creator { get; set; } public int CreatorId { get; set; } public ICollection<EventPhoto> Photos {

我有一个应用程序,用户可以创建一个事件,然后在创建事件后向该事件添加一张照片。这是事件实体:

public class Event
    {
        public int Id { get; set; }
        public AppUser Creator { get; set; }
        public int CreatorId { get; set; }
        public ICollection<EventPhoto> Photos { get; set; }
}
我的控制器中有一个方法,可以将照片添加到现有事件中。这是以前配置方法的方式:

public async Task<ActionResult<bool>> AddPhoto(IFormFile file, int id)
  {
            var existingEvent = await _eventsRepository.GetEventByIdAsync(id);
       
        var result = await _photoService.AddPhotoAsync(file); 

        if (result.Error != null) return BadRequest(result.Error.Message);

             var photo = new EventPhoto // This correctly creates the EventPhoto class and assigns the URL and public id correctly
            {
                Url = result.SecureUrl.AbsoluteUri,
                publicId = result.PublicId

            };

            existingEvent.Photos.Add(photo);

            return await _context.SaveChangesAsync() > 0;

}
public异步任务AddPhoto(ifformfile文件,int-id)
{
var existingEvent=wait _eventsRepository.GetEventByIdAsync(id);
var result=await\u photoService.AddPhotoAsync(文件);
if(result.Error!=null)返回BadRequest(result.Error.Message);
var photo=new EventPhoto//这将正确创建EventPhoto类并正确分配URL和公共id
{
Url=result.SecureUrl.AbsoluteUri,
publicId=result.publicId
};
existingEvent.Photos.Add(照片);
return wait_context.SaveChangesAsync()>0;
}
使用此代码,错误:
对象引用未设置为对象的实例
被抛出到此行:
existingEvent.Photos.Add(photo)

为了防止此错误,我添加了null条件运算符:
existingEvent.Photos?.Add(photo)。这防止了在代码行抛出特定错误,但我遇到的问题是照片实体未添加到现有事件照片中,因此未触发
\u上下文。savechangesync()
,因为实体未发生任何更改


你知道我哪里出错了吗?

你的问题是
现有事件。照片
为空

当您执行
existingEvent.Photos?时,添加(照片)
这只是跳过
Add
的空值

您应该尝试执行以下操作,如果该列表为空,则将分配并创建一个新列表,然后添加
照片

existingEvent.Photos = existingEvent.Photos ?? new List<EventPhoto>();
existingEvent.Photos.Add(photo);
existingEvent.Photos=existingEvent.Photos??新列表();
existingEvent.Photos.Add(照片);

非常感谢!这似乎解决了这个错误。我现在似乎有一个不同的错误,但我将不得不单独调查。只是想让我知道这里发生了什么…这个??空合并运算符检查
存在的事件。照片
是否为空,如果为空,则初始化
新列表。然后,照片可以在初始化后添加到照片列表中。是吗?是的,没错,在这里as
existingEvent.Photos?.Add(photo)
,则code>相当于执行
。感谢您的帮助
existingEvent.Photos = existingEvent.Photos ?? new List<EventPhoto>();
existingEvent.Photos.Add(photo);