Generically correcting data before save with Entity Framework
Posted
by koevoeter
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by koevoeter
Published on Fri, 28 May 2010 12:04:00 GMT
Indexed on
2010/05/28
12:22 UTC
Read the original article
Hit count: 305
Entity Framework
|.NET
Been working with Entity Framework (.NET 4.0) for a week now for a data migration job and needed some code that generically corrects string values in the database. You probably also have seen things like empty strings instead of NULL or non-trimmed texts ("United States ") in "old" databases, and you don't want to apply a correcting function on every column you migrate.
Here's how I've done this (extending the partial class of my ObjectContext):
public partial class MyDatacontext{
partial void OnContextCreated()
{
SavingChanges += OnSavingChanges;
}
{
foreach (var entity in GetPersistingEntities(sender))
{
foreach (var propertyInfo in GetStringProperties(entity))
{
var value = (string)propertyInfo.GetValue(entity, null);
{
continue;
}
{
propertyInfo.SetValue(entity, null, null);
}
else if (value != value.Trim())
{
propertyInfo.SetValue(entity, value.Trim(), null);
}
}
}
}
private IEnumerable<object> GetPersistingEntities(object sender)
{
return ((ObjectContext)sender).ObjectStateManager
.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
.Select(e => e.Entity);
}
private IEnumerable<PropertyInfo> GetStringProperties(object entity)
{
return entity.GetType().GetProperties()
.Where(pi => pi.PropertyType == typeof(string));
}
private bool IsNullable(PropertyInfo propertyInfo)
{
return ((EdmScalarPropertyAttribute)propertyInfo
.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false)
.Single()).IsNullable;
}
}
Obviously you can use similar code for other generic corrections.
© ASP.net Weblogs or respective owner