实体框架附加不更新集合更改

时间:2016-08-19 18:00:25

标签: c# entity-framework

当使用对ID正确设置的用户的引用更新Viewers集合时,不会使用此新引用更新数据库。所有其他字段都已更新,但集合未更新。

var entity = new Event
{
    Id = model.Id,
    Name = model.Name,
    Date = model.Date,
    Location = model.Location,
    EBroadcasters = model.Broadcasters?.Select(b => new User
    {
        Id = b.Id,
        SkypeId = b.SkypeId,
        Name = b.Name,
        ServiceUrl = b.ServiceUrl
    }).ToList(),
    EViewers = model.Viewers?.Select(v => new User
    {
        Id = v.Id,
        SkypeId = v.SkypeId,
        Name = v.Name,
        ServiceUrl = v.ServiceUrl
    }).ToList()
};

_ssRepository.Events.Attach(entity);
_ssRepository.Entry(entity).State = EntityState.Modified;

// Don't think I need to do this stuff but tried everything to get it to update!!!
foreach (var b in entity.EBroadcasters)
{
    _ssRepository.Users.Attach(b);
    _ssRepository.Entry(b).State = EntityState.Modified;
}

foreach (var v in entity.EViewers)
{
    _ssRepository.Users.Attach(b);
    _ssRepository.Entry(v).State = EntityState.Modified;
}

_ssRepository.SaveChanges();

非常感谢任何帮助!

杰拉德

0 个答案:

没有答案