Unable to save null or empty string property

Posts   
 
    
scampbell
User
Posts: 1
Joined: 31-Jan-2024
# Posted on: 31-Jan-2024 21:26:40   

Hello, we are using LLBLGen pro 5.10.2, and we are encountering an issue trying to save an empty string property on an entity, this method worked previously in v4.0. There are no errors or exceptions being thrown that I could find. Database is Microsoft SQL server version ( KB4583458 ) - M15.0.2080.9 (X64)

The simplest example from our code:

public bool SaveEmploymentApplicationStepTwo(EmploymentApplicationModel model)
{
    using (var adapter = new DataAccessAdapter(_config.GetConnectionString("umbracoDbDSN")))
    {
        var employmentApplicationEntity = new EmploymentApplicationEntity(model.ApplicationId)
        {
            AdditionalEducation = model.AdditionalEducation,
        };

        try
        {
            return adapter.SaveEntity(employmentApplicationEntity);
        }
        catch (Exception)
        {
            return false;
        }
    }
}

The types EmploymentApplicationEntity and DataAccessAdapter are both generated by LLBL. AdditionalEducation is nullable in the database and marked as optional in the LLBL designer. There are a few dozen other properties set here in addition to AdditionalEducation, I have removed them from this snippet to better focus on the example.

Steps to reproduce: First enter any text for model.AdditionalEducation, lets say "Certificates", and run the above code. Then update model.AdditionalEducation to be either null or empty string, and run the above code again.

Expected: The AdditionalEducation column on the EmploymentApplication table in the database is updated to be null or empty. Actual: The AdditionalEducation column is unchanged from "Certificates".

Note that if you enter any non-empty string value, like "More certificates", the column updates as expected. Only null and empty strings are doing nothing. I could not find anything in the version changelogs that mention this way of emptying properties being removed. Is this a bug or is there a better way of saving null properties to an entity?

Thank you!

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 01-Feb-2024 08:00:43   

When you create a new instance of the entity, all fields have no values in case of a string (it will be an empty string).

So when you set it to an empty string, the framework doesn't see any change happens to the field, and so it is ignored when being saved.

You can get around this by setting the EntityField.IsChanged to True.

e.g. employmentApplicationEntity .Fields["AdditionalEducation "].IsChanged = true;

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 01-Feb-2024 09:14:55   

Indeed.

Tho I have a question: the code you show is for creating an entity yet the problem you describe is for updating an existing entity? How do you update an existing entity exactly?

Frans Bouma | Lead developer LLBLGen Pro