code_junkie

Entity Framework Guid

Nov 12th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. private Guid _identifier = Guid.NewGuid();
  2.  
  3. void OgaraEntities_SavingChanges(object sender, EventArgs e)
  4. {
  5. foreach (ObjectStateEntry entry in
  6. ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(
  7. EntityState.Added ))
  8. {
  9. if (!entry.IsRelationship){
  10. string keyFieldName = entry.EntitySet.ElementType.KeyMembers[0].Name;
  11. object entity = entry.Entity;
  12. PropertyInfo pi = entity.GetType().GetProperty(keyFieldName);
  13. pi.SetValue(entity, Guid.NewGuid(), null);
  14. }
  15. }
  16. }
Add Comment
Please, Sign In to add comment