Updating an Object sets other DB fields to NULL RRS feed

  • Question

  • Hi there,


    Entity Framework in N-Tier, WCF, SQL Server 2008

    In N-Tier architecture WCF service client creates an instance of Object [Employee], attach that to Service request and gets the data in the form of service request[DTO].

    As objects are returned from the service, updating single property of Object sets other database fields to NULL.

    When a property is updated in the Object from client side, passed to the WCF service, using Entity Framework object is inserted into databse. As other properties[DB columns] are NOT set to any value, they are inserted as NULL in DB table.

    The desired behavior is to just update the property[column] and keep rest same as before.

    Help is appreciated to get rid of this behavior.

    Apriori algorithm [association rule]

    Monday, April 16, 2012 10:49 AM

All replies

  • Hi,

    Some more context could help. Are you using self tracking entities ? It looks like a design flaw in your current updating logic i.e. the object is not marked as being updated so it seems to use the inserted default state and then you get an insertion. If some other fields are set to null, you'll likely have also to tell that changes for these fields should be ignored (so that the NULL values are not written back to the db).

    The smallest amount of code that shows your updating logic could be helpfull in understanding what you are doing wrong if you still need some help to fix this.

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, April 16, 2012 11:24 AM
  • using (var context = Factory.CreateContext())
                    context.ObjectStateManager.ChangeObjectState(entityLocalFileFormatObject, System.Data.EntityState.Modified);

    Thanks for your response.

    Service end

    Note the architecture is N-Tier where Data layer implemented with Entity framework 4.0 using above source to update an entity. Business logic layer is on top of data layer with business rules, then service layer , which calls methods from data layer to implement CRUD operations. WCF Hosting layer exposes service layer. http://dofactory.com/Framework/Framework.aspx

    Client side

    On client side an instance of say Employee object is created, values set and sent back to service to update data as shown below. updating the object SETS unchanged values to NULL in database.


    //create an instance of service client
    EmployeeContractClient client = new EmployeeContractClient();
    //open the proxy
    //Create an instance of Employee object and set [update] data
    Employee emp = new Employee();
    emp.PK = 20;
    emp.CreatedBy = "S2";
    emp.Age = "53";
    emp.TermTypeFK = 2;
    //create an instance of request and access service method
        EmployeeRequest requestemp = new EmployeeRequest
                    Employee = emp,
                    Action = ActionType.Update
                var output = client.SetEmployee(requestemp);
                if (output.Acknowledge == AcknowledgeType.Success)
                    Response.Write("Record updated");

    In above code even though 3 columns being updated, another 5 columns[unchanged] in the database have been updated to NULL.

    Apriori algorithm [association rule]

    Monday, April 16, 2012 4:03 PM