更新实体flush是EF中的子实体

时间:2015-07-14 14:56:34

标签: c# entity-framework

当我在项目中保存现有实体时,如果我没有在DbSet上包含子项,则会丢失之前的值。我无法弄清楚这个项目有什么问题,我不必在我的其他项目中这样做。

一些代码:

var quoteRequest = _session.Set<QuoteRequest>()
                .Include(x => x.QuoteRequestInvites)
                .Include(x => x.QuoteRequestInvites.Select(y => y.SelectedService))
                .Include(x => x.QuoteRequestInvites.Select(y => y.SelectedService).Select(z => z.Service))
                .Include(x => x.QuoteRequestInvites.Select(y => y.Provider))
                .Include(x => x.District)
                .Include(x => x.City)
                .FirstOrDefault(x => x.Id == quoteRequestId);

if (quoteRequest == null)
    return Request.CreateResponse(HttpStatusCode.NotFound);

foreach (var invite in quoteRequest.QuoteRequestInvites)
{
    if (invite.Token == Guid.Empty)
    {
        _logger.Warn(invite, "Empty token changed for invite " + invite.Id);
                    invite.Token = Guid.NewGuid();
                    _session.Commit();
    }
}

_providerMailerService.ProcessInvites(quoteRequest);
_customerMailerService.ProcessNotification(quoteRequest);

_session.Attach(administrator);
quoteRequest.ApprovalDate = DateTime.UtcNow;
quoteRequest.ApprovedBy = administrator;
_session.Commit();

问题在于项目中某处我们缺少某些包含,因此我们会丢失数据。

0 个答案:

没有答案