I am using an ADO.NET Entity-Framework ObjectContext to access my data store.
I want the if values are set with empty strings, they should automatically become null.
I've just adapted the code above to the new release of Entity Framework 4.1 (DbContext).
public override int SaveChanges()
{
var objContext = ((IObjectContextAdapter)this).ObjectContext;
var entries = objContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified).Select(
entry => entry.Entity);
foreach (var entity in entries)
{
string str = typeof(string).Name;
var properties = from p in entity.GetType().GetProperties()
where p.PropertyType.Name == str
select p;
foreach (var item in properties)
{
string value = (string)item.GetValue(entity, null);
if (value != null && value.Trim().Length == 0)
{
item.SetValue(entity, null, null);
}
}
}
return base.SaveChanges();