I use EF 4.3.1 I need adding a Default Value of an entity at the moment of saving.
At the moment I’m using SaveChanges() and it works.
As you can see from the code I use ChangeTracker.Entries<Option>() where Option rappresent a specifc DataType.
I would like to know if is possible and how to write a more generic version of ChangeTracker.Entries something like ChangeTracker.Entries<t>() so it can check all type of entities in my Model, as you can see in my specif case I have a NoteInternal property on many classes
public override int SaveChanges()
{
#region Option BL
var entities = ChangeTracker.Entries<Option>()
.Where(e => e.State == EntityState.Added ||
e.State == EntityState.Modified)
.Select(e => e.Entity);
// Add Default values when Creating or Editing an Entity
string defaultvalue = "";
foreach (var entity in entities)
{
if (String.IsNullOrWhiteSpace(entity.NoteInternal))
entity.NoteInternal = defaultvalue;
}
#endregion
return base.SaveChanges();
}
Thanks for your help!
PS: does it make sense use the DYNAMIC datatype here?
I can think of three options:
NoteInternalproperty. This is slow.dynamicto try to access theNotIntenralproperty of each item. You have to check for and handle the exception thrown when the entity does not have the property.IHasNoteInternalthat defines the property and have all entities that has the property implement that interface. Then get all entries ofIHasNoteInternaltype.I think that the 3. option is best and gives the best design, but it requires you to change the entities which migh or might not be possible. I don’t know if you can use the generic
Entries<TEntity>()method to get the changed entities – it might fail to match the entities based on an interface. In that case, use the non-genericEntries()and use linq’sOfType<T>()operator to filter out the relevant entries.