-1

How do you update a subtable value with Lambda EF core.

var UpdatePercentage = _context.TaskOverview.Single(c => c.Id == 100);
UpdatePercentage.Completedpercentage = 30;
UpdatePercentage.TaskSubInfo.Content1 = "Some value";

await _context.SaveChangesAsync();

I get NullReferenceException error

NullReferenceException: Object reference not set to an instance of an object.
Sid
  • 14,176
  • 7
  • 40
  • 48
David
  • 31
  • 9
  • on which of your three lines do you get your exception: is your _context null? or is UpdatePercentage.CompletedPercentage null? Or perhaps UpdatePercentage.TaskSubInfo is null? – Harald Coppoolse Dec 05 '16 at 11:10
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – kiziu Dec 05 '16 at 11:12

2 Answers2

0

You can use Include

var UpdatePercentage = _context.TaskOverview.Include("TaskSubInfo").Single(c => c.Id == 100);
0

I guess you are getting the NullReferenceException on the related Entity TaskSubInfo.

EntityFramework doesnt load related Entity by default but you have to do it

var UpdatePercentage = _context.TaskOverview
                            .Include(to => to.TaskSubInfo)
                            .Single(c => c.Id == 100);

UpdatePercentage?.Completedpercentage = 30;
UpdatePercentage?.TaskSubInfo.Content1 = "Some value";

await _context.SaveChangesAsync(); 
Sid
  • 14,176
  • 7
  • 40
  • 48