3

When I save an existing entity in my project, if I don't include the child on the DbSet, it lose the previous value. I can't figure out what's wrong with this project, I do not have to do this in my others projects.

Some code :

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();

The problem is that there is somewhere in the project where we are missing some includes, so we lose data.

Glen Thomas
  • 8,455
  • 5
  • 24
  • 60
VinnyG
  • 6,743
  • 7
  • 54
  • 73
  • Can you provide details: what data you loose, what include was missed, what is the model. Can you simplify and reproduce the problem? – astef Jul 14 '15 at 15:03
  • The problem is that if I don't do .Include on the child entity, I loose it on an update. Normaly, I don't need to do Include on all children, can't find what's the problem in this projecct and I'm wondering if someone else had the same problem (and a solution). – VinnyG Jul 14 '15 at 15:15
  • **Eager loading** is typically more efficient when you need the related data for all retrieved rows of the primary table and also when relations are not **too much**, eager loading will be good practice to reduce further queries on server. But since it seems your relations are **too much** I think **lazy loading** maybe a good choice here. Check this post for more details: http://stackoverflow.com/questions/34627865/eager-lazy-and-explicit-loading-in-ef6 – Salah Akbari Apr 05 '16 at 06:27
  • Have you tried to log the generated sql query sent to the DB using a logger like: private static void SetLogger(this DbContext context) { #if DEBUG ILog logger = LogProvider.GetCurrentClassLogger(); context.Database.Log = s => logger.Debug(s); #endif } – Alegrowin Apr 05 '16 at 13:53
  • @s.Akbari I tought lazy loading is by default? – VinnyG Apr 05 '16 at 23:22
  • @S.Akbari using Include does not mean that you have disable lazy loading. You can use Include with lazy loading enabled. In the code above you run a single query instead of 7. Then, if you access to a "not Included" property EF reads it with a new query. – bubi Apr 06 '16 at 06:01
  • You are speaking about a normal EF behaviour. If you have an object and for some reason you Clear a related collection EF generates a delete query or an update query (set the reference column to the parent table to null) for the children. If you answer to @astef question I'm sure you will receive more usefull help – bubi Apr 06 '16 at 06:09
  • @VinnyG Not sure if this helps, but sometimes a clean and rebuild does the trick! In some cases I worked on, that resolved the issues I was facing. – Andrew Apr 11 '16 at 18:47

0 Answers0