none
Update related entities 4.1 RRS feed

  • Question

  • In a db first model (I think I am the only one using db first anymore...)  and using DBContext, I have a Case entity and an Event entity.

    Case.CaseID PK

    Event.EventID PK

    Event.CaseID FK

     

    I want to update both of the entities using code like this:

     

      CaseManagementEntities db = new CaseManagementEntities();
               
                var originalCase = db.Cases.Find(c.CaseID);
                var originalEvent =              
                    from events in db.Events
                    where events.CaseID == c.CaseID
                    select events;
                
    
                db.Entry(originalCase).CurrentValues.SetValues(c);
                db.Entry(originalEvent).CurrentValues.SetValues(e);
    
    
        db.SaveChanges();
              

    c is a updated Case and e is an updated Event from the web form.

     

    When hitting the
    db.Entry(originalEvent).CurrentValues.SetValues(e); line I get the error

    The entity type DbQuery`1 is not part of the model for the current context.

     

    Any ideas on what is wrong?

    Wednesday, January 25, 2012 7:56 PM

Answers

  • Hi forwheeler;

    The issue is that the DbContext.Entry method takes a single entity as a parameter but you are assigning a anonymous Linq query to it which the compiler is calling DbQuery`1. This is because you are not executing the query you are just defining it. In order to execute it you need to enumerate over it or use a function that will do that as part of its function such as FirstOrDefault. But this will have an issue because that query returns a collection of db.Events and using the first event but it may not be the one you have modified so you may have to change your query as follows to get the right event from the query.

    CaseManagementEntities db = new CaseManagementEntities();
               
    var originalCase = db.Cases.Find(c.CaseID);
    var originalEvent =              
        (from events in db.Events
         where events.CaseID == c.CaseID && events.EventID == e.EventID 
         select events).FirstOrDefault();
    
    
    db.Entry(originalCase).CurrentValues.SetValues(c);
    db.Entry(originalEvent).CurrentValues.SetValues(e);
    
    
    db.SaveChanges();
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by forwheeler Thursday, January 26, 2012 1:48 PM
    Wednesday, January 25, 2012 9:23 PM

All replies

  • Hi forwheeler;

    The issue is that the DbContext.Entry method takes a single entity as a parameter but you are assigning a anonymous Linq query to it which the compiler is calling DbQuery`1. This is because you are not executing the query you are just defining it. In order to execute it you need to enumerate over it or use a function that will do that as part of its function such as FirstOrDefault. But this will have an issue because that query returns a collection of db.Events and using the first event but it may not be the one you have modified so you may have to change your query as follows to get the right event from the query.

    CaseManagementEntities db = new CaseManagementEntities();
               
    var originalCase = db.Cases.Find(c.CaseID);
    var originalEvent =              
        (from events in db.Events
         where events.CaseID == c.CaseID && events.EventID == e.EventID 
         select events).FirstOrDefault();
    
    
    db.Entry(originalCase).CurrentValues.SetValues(c);
    db.Entry(originalEvent).CurrentValues.SetValues(e);
    
    
    db.SaveChanges();
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by forwheeler Thursday, January 26, 2012 1:48 PM
    Wednesday, January 25, 2012 9:23 PM
  • Yes of course, thanks
    Thursday, January 26, 2012 1:48 PM
  •  

    Not a problem forwheeler, glad I was able to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, January 26, 2012 4:08 PM