Updating Nullable Foreign Key To Null

Posts   
 
    
JoshWright
User
Posts: 12
Joined: 05-Sep-2008
# Posted on: 09-Oct-2008 21:21:56   

We've got a web page that allows users to edit a large number of addresses. For the sake of speed, we're not fetching addresses before we update them. Instead we store the id of the entity in ViewState, then rebuild the entity on a postback by creating a new address entity, setting the Id to its old value, and setting the IsNew flag to false.

The address entity has a nullable foreign key to StateId, which is a string. We're working in the Adapter pattern (2.6). Here's a simplified example of what we're doing.


AddressEntity address = new AddressEntity();
address.IsNew = false;
address.Id = oldAddress.Id;

//Update the selected state 
if(String.IsNullOrEmpty(ddlStateId.SelectedValue))
{
    address.StateId = null;
}
else
{
    address.StateId = ddlStateId.SelectedValue;
}

adapter.SaveEntity(address, true, true);

The Problem

If StateId's previous value was "CA" and is changed to null, this code will not change the foreign key in the database. The foreign key will maintain its previous value.

I guess the reason it doesn't work is the address entity doesn't know StateId changed... when you set it to null it is already null (because it isn't fetched). So the only way to get this to work is to do something like this:


    address.StateId = "XX";  //Force address to know StateId is updated
    address.StateId = null;

This way the address entity knows StateId was updated.

I don't know if this is a bug or just a feature that would be impossible to implement, but I thought I'd bring it up in case someone else has the same problem.

If I understand the problem correctly, it only effects people who are: - using the Adapter pattern and - updating an entity directly (meaning it's not fetched) and - have a nullable foreign key (of type "String"?) and - are changing the value from a key to null.

Pretty tight parameters. Here's the test we wrote (when we were debugging) that we'd like to pass:


[Test]
public void UpdatingForeignKeysToNullWorks()
{
    using (var adapter = new DataAccessAdapter())
    {
        AddressEntity oldAddress = EntityBuilders.BuildAddressEntity();
        oldAddress.State.Id = "OK";

        adapter.SaveEntity(oldAddress, true, true);

        AddressEntity address = new AddressEntity();
        address.IsNew = false;
        address.Id = oldAddress.Id;

        //address.StateId = "XX";  //this line shouldn't be required
        address.StateId = null;

        adapter.SaveEntity(address, true, true);

        Assert.IsNull(address.StateId);
        Assert.AreEqual(oldAddress.Id, address.Id);
    }
}

By the way, LLBLGen is a fantastic product and I would highly recommend it to anyone looking for an ORM.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Oct-2008 05:39:48   

JoshWright wrote:

I guess the reason it doesn't work is the address entity doesn't know StateId changed... when you set it to null it is already null (because it isn't fetched).

Indeed simple_smile

You can workaround this artificially, indicating that the field has been changed:

//Update the selected state
if(String.IsNullOrEmpty(ddlStateId.SelectedValue))
{
    address.StateId = null;
    address.Fields["StateId"].IsChanged = true;

}
David Elizondo | LLBLGen Support Team