locked
Entity Framework AddObject not Working RRS feed

  • Question

  • I am trying to add a new entity via add object and its not persisting the entity to the database but it is neither throwing an error.

    validMessage =

    AuditMessage.CreateAuditMessage(1,Guid.NewGuid(),"<Test>This is a test request</Test>",DateTime.Now,"Test");

    after creating the context I do this

    modelContext.AuditMessages.AddObject(Validmessage);

     

     

     

    if

    (modelContext.AuditMessages.Count() > 0)

    {

    modelContext.SaveChanges();

    }

     

    else{throw new Exception

    ();}

     

     

     

     

     

    I have noticed the count property is 0 after I call the add object method. 

    Help I need to get this turned around quick.


    Mteasdal
    Friday, March 25, 2011 2:29 PM

Answers

  • If there is no record in your AUditMessage table or the table which represent AuditMessages object then 

    modelContext.AuditMessage.Count() will always return 0. 

    In that case modelContext.AuditMessage.Count() > 0 will be always false, thus modelContext.SaveChanges() will never execute. 

    Hence your addobject will be never be persisted in the database permanently. 

     

    why you need to check modelContext.AuditMessage.Count()>0 

     

    Simply following should do your work 

     

    modelContext.AuditMessages.AddObject(auditMessage); 

    modelContext.SaveChanges(); 

     

     

     

     


    Tanvir Huda
    • Marked as answer by Yi-Lun Luo Thursday, March 31, 2011 9:14 AM
    Sunday, March 27, 2011 12:40 PM

All replies

  • If there is no record in your AUditMessage table or the table which represent AuditMessages object then 

    modelContext.AuditMessage.Count() will always return 0. 

    In that case modelContext.AuditMessage.Count() > 0 will be always false, thus modelContext.SaveChanges() will never execute. 

    Hence your addobject will be never be persisted in the database permanently. 

     

    why you need to check modelContext.AuditMessage.Count()>0 

     

    Simply following should do your work 

     

    modelContext.AuditMessages.AddObject(auditMessage); 

    modelContext.SaveChanges(); 

     

     

     

     


    Tanvir Huda
    • Marked as answer by Yi-Lun Luo Thursday, March 31, 2011 9:14 AM
    Sunday, March 27, 2011 12:40 PM
  • Hello,

     

    I am struggling with a similar problem. I am intentionally trying not to call SaveChanges() right away, and instead am trying to bind a DetailsView to the newly created object thus allowing the user to review values in object properties prior to saving them to the database. But, just like the original poster, I am having trouble seeing the object after I add it to the EntitySet.

     

    So is the SaveChanges step absolutely necessary in order for the new object to be properly added, or is there a way to add it in-memory prior to persisting it to the DB?

     

    Thanks!

    Thursday, June 9, 2011 4:33 AM