Entity Framework navigation properties do not load until a new DbContext is installed

I'm still relatively new to EF, so forgive me if I lack an obvious concept.

Let me see if I can simplify this ... The old question is in the history of changes, but I think I can get rid of it:

FWIW, DbContext- REQUEST, not static, therefore the first example works. Not using DI / IoC on the controller at this point.

public class OrdersController : ApiController {
    private MyDbContext db = new MyDbContext();

    //controller methods....

    protected override void Dispose(bool disposing) {
      db.Dispose();
    }
}

Works (2 separate requests):

//request 1: client code sends in a new order to be added to db
public Order Post([FromBody]Order order) {
  db.Orders.Add(order);
  db.SaveChanges();

  return order;
}

//request 2: someone punches a button to send an email to CS about this order
public void NotifyCustomerService(int orderid) {
  var order = db.Orders.Find(orderid);
  //do email code here
}

Broken (single request):

//request: client code sends in a new order to be added to db AND notifies CS at same time
public Order Post([FromBody]Order order) {
  db.Orders.Add(order);
  db.SaveChanges();

  //notify CS via email here (nav properties are not populating)

  return order;
}

Works (one request) (but I know this is a terrible practice):

//request: client code sends in a new order to be added to db AND notifies CS at same time (using a new dbcontext in the notification code)
public Order Post([FromBody]Order order) {
  db.Orders.Add(order);
  db.SaveChanges();

  using(var db2 = new MyDbContext()) {
    var sameOrderWTF = db.Orders.Find(order.ID);
    //notify CS via email using sameOrderWTF instance here (nav properties ARE populating)
  }
  return order;
}

, , , nav . DbContext... , , in-mem, . , .

//request: client code sends in a new order to be added to db AND notifies CS at same time
public Order Post([FromBody]Order o) {
  Order order = db.Orders.Create();
  db.Orders.Add(order);

  //copy values from input to proxy instance
  db.Entry(order).CurrentValues.SetValues(o);

  //copy input lines to proxy instance (same process as order for each line)
  o.OrderLines.ToList().ForEach(l => {
    var line = db.OrderLines.Create();
    db.OrderLines.Add(line);
    db.Entry(line).CurrentValues.SetValues(l);
    order.OrderLines.Add(line);
  });

  db.SaveChanges();

  //notify CS via email here (nav properties are not populating)

  return order;
}

, , ( Uber Bot), , ( ) ASP.NET MVC EF. , ViewModels VM , , EF, .

+4
2

-, .

.

db.Entry(order).Reference(o => o.YouProperty).Load();

, factory, .

db.Orders.Create();

, . , (, - ), , Create DbSet.

https://msdn.microsoft.com/en-us/data/jj592886.aspx

+2

FYI... , ..... .

//only handles new orders...as is
//Assumes!!! Order is of a type which is a db Entity! (Table)
//Assumes that you populated "order" with all the required properties.

public Order Post([FromBody]Order order) 
{
    db.Orders.Add(order);
    db.SaveChanges();
    return order;
}

.... , "" db, , , , . OrderDTO - , db Order. .

0

Source: https://habr.com/ru/post/1611674/


All Articles