Hi
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.
Hi
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.
Not that I'm aware of.
You could possibly write a class that inherited from ObjectContext
and override SaveChanges()
to do that and use that instead of ObjectContext
in your x.objectlayer.cs / x.designer.cs
I added this to the ObjectContext partial class. It does work, if you have better ideas, please post!
partial class MyObjectContext
{
partial void OnContextCreated()
{
this.SavingChanges += new EventHandler(Entities_SavingChanges);
}
void Entities_SavingChanges(object sender, EventArgs e)
{
foreach (EntityObject entity in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified).Select(entry => entry.Entity))
{
if (entity == null || entity is User) continue;
string str = typeof(string).Name;
var props = entity.GetType().GetProperties();
var properties = from p in entity.GetType().GetProperties()
where
p.PropertyType.Name == str &&
p.IsDefined(typeof(EdmScalarPropertyAttribute), false) &&
p.IsDefined(typeof(DataMemberAttribute), false)
select p;
foreach (var item in properties)
{
string value = (string)item.GetValue(entity, null);
if (value != null && value.Trim().Length == 0)
entity.GetType().GetField("_" + item.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(entity, null);
}
}
}
}