// Interact With Shadow Property public override int SaveChanges() { ChangeTracker.DetectChanges(); var timestamp = DateTime.Now; foreach (var entry in ChangeTracker.Entries() .Where(e => (e.State == EntityState.Added || e.State == EntityState.Modified) && !e.Metadata.IsOwned())) { entry.Property("LastModified").CurrentValue = timestamp; if (entry.State == EntityState.Added) { entry.Property("Created").CurrentValue = timestamp; } if (entry.Entity is Samurai) { if (entry.Reference("BetterName").CurrentValue == null) { entry.Reference("BetterName").CurrentValue = PersonFullName.Empty(); } entry.Reference("BetterName").TargetEntry.State = entry.State; } } return(base.SaveChanges()); }
public override int SaveChanges() { ChangeTracker.DetectChanges(); var timestamp = DateTime.Now; var copy = ChangeTracker.Entries() .Where(e => (e.State == EntityState.Added || e.State == EntityState.Modified) && !e.Metadata.IsOwned()); // var id = //metadata owned types are treated as regular entries to differ one from another //must check in metadata //problem with workaround alter element inside foreach loop try{ foreach (var entry in copy) { entry.Property("LastModified").CurrentValue = timestamp; if (entry.State == EntityState.Added) { entry.Property("Created").CurrentValue = timestamp; } if (entry.Entity is Samurai) { if (entry.Reference("BetterName").CurrentValue == null) { entry.Reference("BetterName").CurrentValue = PersonFullName.Empty(); Console.WriteLine("Name:", entry); // copy2. } entry.Reference("BetterName").TargetEntry.State = entry.State; } } } catch (Exception e) { // } // ChangeTracker= return(base.SaveChanges()); }