Is there an option to make Entity Framework revert

2019-03-16 04:41发布

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.

8条回答
干净又极端
2楼-- · 2019-03-16 05:01

Here is a solution for Entity Framework Core (tested in V2). Had to bang my way through the API due to limited documentation so there might be other ways to accomplish the same thing. Note that the original objects are modified using this method.

public override int SaveChanges()
{
    ConvertWhitespaceToNulls();
    return base.SaveChanges();
}


private void ConvertWhitespaceToNulls()
{
    var entityEntries = this.ChangeTracker
        .Entries()
        .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added && x.Entity != null);

    foreach (var e in entityEntries)
        foreach (var currentValue in e.CurrentValues.Properties.Where(p => p.ClrType == typeof(string) && p.IsNullable))
            if (string.IsNullOrWhiteSpace((string) currentValue.FieldInfo.GetValue(e.Entity)))
                currentValue.FieldInfo.SetValue(e.Entity, null);
}
查看更多
Rolldiameter
3楼-- · 2019-03-16 05:04

Just for completeness, here is the accepted answer written as a partial class instead of inherited. Note that this version also trims strings.

using System.Data;
using System.Data.Objects;
using System.Linq; 
public partial class MyObjectContext {
    private const string StringType = "String";
    private const EntityState SavingState = EntityState.Added | EntityState.Modified;

    public override int SaveChanges(SaveOptions options) {
        var savingEntries = this.ObjectStateManager.GetObjectStateEntries(SavingState);

        foreach (var entry in savingEntries) {
            var curValues = entry.CurrentValues;
            var fieldMetadata = curValues.DataRecordInfo.FieldMetadata;
            var stringFields = fieldMetadata.Where(f => f.FieldType.TypeUsage
                                                         .EdmType.Name == StringType);

            foreach (var stringField in stringFields) {
                var ordinal = stringField.Ordinal;
                var curValue = curValues[ordinal] as string;

                if (curValue != null && curValue.All(char.IsWhiteSpace)) {
                    curValues.SetValue(ordinal, null);
                }
                else if (curValue != null && curValue != curValue.Trim()) {
                    curValues.SetValue(ordinal, curValue.Trim());
                }
            }
        }
        return base.SaveChanges(options);
    }
}

I have also shown the required usings because I find it frustrating when I copy'n'paste code and the IDE throws up some errors about type or namespace not found.

查看更多
在下西门庆
4楼-- · 2019-03-16 05:06

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();
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-16 05:10

If your using Entity Framework 4, you can use T4 templates to accomplish this. Just place this in the getter of each string property in your .tt template file, and it will replace empty strings with null and automatically trim strings. No need to use reflection.

<#+ if (primitiveProperty.TypeUsage.ToString().Split('.').Last() == "String") { #>
   if (value == "") value = null;
   else value = value.Trim();
<#+ } #>
查看更多
Anthone
6楼-- · 2019-03-16 05:16

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

查看更多
一夜七次
7楼-- · 2019-03-16 05:17

I actually found a better way to this, it's actually built in the system, plus it uses the internal ordinal metadata of the entities which are loaded anyway (I haven't tested the performance difference, but this should be hell of a lot faster than reflection):

private const string StringType = "String";
private const EntityState SavingState = EntityState.Added | EntityState.Modified;
public override int SaveChanges()
{
  //when using on ObjectContext replace 'objectContext' with 'this',
  //and override SaveChanges(SaveOptions options) instead:

  var objectContext = ((IObjectContextAdapter)this).ObjectContext;
  var savingEntries = objectContext.ObjectStateManager
    .GetObjectStateEntries(SavingState);

  foreach (var entry in savingEntries)
  {
    var curValues = entry.CurrentValues;
    var fieldMetadata = curValues.DataRecordInfo.FieldMetadata;
    var stringFields = fieldMetadata
      .Where(f => f.FieldType.TypeUsage.EdmType.Name == StringType);
    foreach (var stringField in stringFields)
    {
      var ordinal = stringField.Ordinal;
      var curValue = curValues[ordinal] as string;
      if (curValue != null && curValue.All(char.IsWhiteSpace))
        curValues.SetValue(ordinal, null);
    }
  }
  return base.SaveChanges(); //SaveChanges(options) on ObjectContext
}
查看更多
登录 后发表回答