1

Changes to nullable bool properties are not saved back to the db in EF4 however other fields which are nullable are updating without any issues. For example, if I execute simple query similar to the following:

EmployeeSurvey employeeSurvey = context.EmployeeSurveys.SingleOrDefault(s => s.EmployeeSurveyID == 60);

employeeSurvey.EmployeeSmokes = true; 
employeeSurvey.OtherComments = "Test comment";

context.SaveChanges();

The OtherComments changes are successfully saved back to the db however the EmployeeSmokes property changes are not. The EmployeeSmokes property is a bool? and other nullable boolean fields have the same issue.

Additionally, the problem only occurs when changing/updating existing EmployeeSurvery records - all properties including EmployeeSmokes are successfully saved when creating/inserting new EmployeeSurveys.

I've also tried using the ApplyCurrentValues method as per this thread but unfortunately it hasnt helped.

Any ideas why this is happening?

Community
  • 1
  • 1
Steve
  • 11
  • 2
  • Your code is correct. The problem is elsewhere (not in your question). This works for me. You need to debug this more. Try SQL tracing. – Craig Stuntz Sep 01 '10 at 12:43
  • I'm experiencing the same problem like you, but it is for nullable integers. From what I've managed to test, when you add the entity in the database and the field is set to **null**, you won't be able to update it using EF after that... So I suppose this is some kind of bug. I'll continue searching and I'll post an answer if I find something. – Ivan Nikolov Jul 04 '11 at 07:43

2 Answers2

0

What is the value of employeeSurvey.EmployeeSmokes in the Database? If it is true, EF will notice there is no change and omit it från the generated Update SQL since there is no change (you can verify this in the SQL Profiler).

0

A few minutes after posting a comment I found a solution to my problem, that might help you too if you still need this.

I'm using self tracking entities and I had to add some code in the generated template. In the UpdateOriginalValues(ObjectContext context, IObjectWithChangeTracker entity) method I added the following fragment:

foreach(EdmProperty property in entityType.Properties)
    {
        object value;
        if(property.TypeUsage.EdmType is PrimitiveType && entity.ChangeTracker.OriginalValues.TryGetValue(property.Name, out value))
        {
            //START OF EDIT
            if (value == null && property.Nullable)
            {
                var currentValues = entry.CurrentValues;
                int ordinal = currentValues.GetOrdinal(property.Name);
                var temp = currentValues[ordinal];
                currentValues.SetDBNull(ordinal);
                entry.ApplyOriginalValues(entity);
                currentValues.SetValue(ordinal, temp);
            }
            //END OF EDIT
            originalValueRecord.SetValue(property, value);
        }
        else if(property.TypeUsage.EdmType is ComplexType)
        {
            OriginalValueRecord complexOriginalValues = originalValueRecord.GetOriginalValueRecord(property.Name);
            UpdateOriginalValues((ComplexType)property.TypeUsage.EdmType, entity.GetType().FullName, property.Name, entity.ChangeTracker.OriginalValues, complexOriginalValues);
        }
    }

The original source is HERE. I hope this will be useful!

Ivan Nikolov
  • 793
  • 4
  • 15