Override DBContxt. SaveChanges method to generate the Keys and populate our objects.
When we populate Parent class Entity Framework will take care of pushing that value to all children.
a. Since we want to get all our keys at once we need to figure out how many new objects we have and what type of the object that is. DbContext has ChangeTracker property that can help us here. Since we inherited our classes from SequenceBase we can get to all new SequenceBase Entities using simple selector
foreach (var entry in this.ChangeTracker.Entries().Where(e => e.Entity is SequenceBase && e.State == EntityState.Added))
We can find entity type by simply doing entry.Entity.GetType().Name
b. Next we need To Go to DB Generate Keys
c. After we get the keys from DB we need to assign them to primary key of the object. This is a little tricky and there are few ways of doing it. One way was to use Reflection based off Object Context but Julie recommended using MetadataWorkspace that worked quite nicely.
var wKey = w.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets.First(meta => meta.ElementType.Name == entityName)
.ElementType.KeyMembers.Select(k => k.Name).FirstOrDefault();v
d. After we have a Key Filed name life is good because we can simply set it to new key
entry.Property(wKey).CurrentValue = newKey;