Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework: complains of null id when entity id is set in code during SaveChanges
    primarykey
    data
    text
    <p>Lets assume the following code handling the SaveChanges event of a DataContext</p> <pre><code>void Context_SavingChanges(object sender, EventArgs e) { IEnumerable&lt;ObjectStateEntry&gt; objectStateEntries = from ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted) where ose.Entity != null select ose; foreach (ObjectStateEntry entry in objectStateEntries) { foreach (var field in entry.CurrentValues.DataRecordInfo.FieldMetadata) { var guid = Guid.NewGuid(); AuditEntry audit = AuditEntry.CreateAuditEntry( id: guid, entitySet: entry.EntitySet.Name, typeName: entry.Entity.GetType().Name, entityId: (entry.CurrentValues["Id"] as string) ?? ((entry.State == EntityState.Added) ? "New" : null), oldValue: (entry.State != EntityState.Added) ? entry.OriginalValues[field.FieldType.Name].ToString() : "New", newValue: (entry.State != EntityState.Deleted) ? entry.CurrentValues[field.FieldType.Name].ToString() : "Deleted", modifier: Environment.UserDomainName + "\\" + Environment.UserName, dbAction: Enum.GetName(typeof(EntityState), entry.State), field: field.FieldType.Name, dateStamp: DateTime.Now ); this.AuditEntries.AddObject(audit); } } } </code></pre> <p>When I create a new Entity and attempt to save it's changes I get this error:</p> <blockquote> <p>Cannot insert the value NULL into column 'Id', table 'TimeEF.dbo.AuditEntries'; column does not allow nulls. INSERT fails. The statement has been terminated.</p> </blockquote> <p>When as you can see I have set the Id, any ideas? Bug?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload