Null created_by and modified_by fields

Aug 18, 2010 at 2:29 PM
I have created a small ASP.NET test website using nHydrate and I'm having problems with the created_by and modified_by columns not getting set on the entity or the audit records using EFDAL. The site uses Windows authentication and impersonation. I have set the user in the ContextStartup (inside the debugger, I see the user's name getting passed into the ContextStartup), entities and audit records are getting written to the DB and things seem to be working fine, it's just that created_by and modified_by are always Null. I saw the recent post about a smilar problem on Jul 28th that was fixed by a new build. I have reinstalled nHydrate (v3.6), regenerated, and rebuild the project, but the problem still remains. Does anybody have any other thoughts or suggestions? I'm including the code for the webform submit button handler, which is responsible for all object creates and edits, am I doing something wrong? Thanks. protected void btnSubmit_Click(object sender, EventArgs e) { ContextStartup userStartup = new ContextStartup(Request.ServerVariables["LOGON_USER"]); using (TestEntities context = new TestEntities(userStartup)) { if (!string.IsNullOrEmpty(Request.QueryString["id"])) { Guid id = new Guid(Request.QueryString["id"]); var contact = context.Contacts.Single(c => c.Id == id); contact.FirstName = txtFirstName.Text; contact.LastName = txtLastName.Text; contact.EmailAddress = txtEmailAddress.Text; } else { context.Contact.AddObject(new Contacts() { FirstName = txtFirstName.Text, LastName = txtLastName.Text, EmailAddress = txtEmailAddress.Text }); } context.SaveChanges(); } }
Aug 18, 2010 at 2:36 PM

Sorry about the lack of formatting, does this work any better.

protected void btnSubmit_Click(object sender, EventArgs e)
{ 
   ContextStartup userStartup = new ContextStartup(Request.ServerVariables["LOGON_USER"]);
   using (TestEntities context = new TestEntities(userStartup))
   { 
      if (!string.IsNullOrEmpty(Request.QueryString["id"]))
      {
         Guid id = new Guid(Request.QueryString["id"]); 
         var contact = context.Contacts.Single(c => c.Id == id);
         contact.FirstName = txtFirstName.Text; contact.LastName = txtLastName.Text;
         contact.EmailAddress = txtEmailAddress.Text;
      }
      else
      { 
         context.Contact.AddObject(new Contacts() { FirstName = txtFirstName.Text, LastName = txtLastName.Text, EmailAddress = txtEmailAddress.Text });
      }
      context.SaveChanges();
   }
} 
Aug 20, 2010 at 4:31 PM

OK, after debugging through the generated code further, I guess I should have used context.AddItem() instead of context.Contact.AddObject() (which in hindsight is exactly what the codeproject nHydrate EFDAL example shows).  The latter does sucessfully persist the entity to the DB but doesn't use the ContextStartup.Modifier and then subsequent audits records have those fields null, becuase they were null on the initial save.  Being completely new to EF, I'd appreciate any input on when I'd use AddItem vs AddObject?  In case anybody else makes this mistake, here's the correction.  Thanks.

context.AddItem(new Contacts() { FirstName = txtFirstName.Text, LastName = txtLastName.Text, EmailAddress = txtEmailAddress.Text });