简体   繁体   中英

Use default value in SQL if string in entity is null

I have a project with database first approach and a SQL Server database.

In one of tables I have a not nullable column with a default value as empty string:

[Comments] NVARCHAR(1024) NOT NULL DEFAULT ('')

Default value here should always be an empty string to completely support odata "Is not empty" filter which otherwise returns me records with null values which are really empty from business perspective.

In my model I also have a string property for this column:

public string Comments { get; set; }

Now what happens is: from frontend I may get null value if appropriate input is not filled with data and this value will be mapped into the entity which is then going to be inserted by EF.

In case if I just try to set a record with null "Comments" property instead of getting default value on SQL side I'm getting an exception.

I tried to fix it by setting StoreGeneratedPattern for this column to Computed in EDMX. Now I can insert records with nulls and I get default value but I get it every time, even if have a value in entity.

In other words computed StoreGeneratedPattern always ignores value of this property in entity and disabling StoreGeneratedPattern basically leaves me with no default value on the SQL side.

What I want is to solve this issue on backend in some nice way.

  1. I don't want to solve it by just always sending empty string from frontend and adding validation to the backend that this property is never null. It will force me to do the same for other clients (mobile client for example) and for every form and table where I meet the same situation.

  2. I don't want to solve it by just doing something like:

     entity.Comments = entity.Comments ?? string.empty; 

    on the backend. It will force me to do it in every place where I process this and other similar entities.

  3. I don't want to solve it by using some interface for similar entities or by initialization of this property in constructor. I have one single T4 template which builds entity models for me from EDMX for decoupling reasons so all models are autogenerated and should never be customized.

The behaviour which I was expecting and which I would like to reach is as follows:

if EF tries to insert a record with null value for this column, SQL Server uses DEFAULT, otherwise it inserts what the entity had.

Any ideas or solutions would be much appreciated.

I have a generic function I use for such tasks in my CRUD functions

private T SetNullStringToDefault<T>(T entity, string default)
{
    List<PropertyInfo> properties = entity.GetType().GetProperties().ToList();
    foreach(var property in properties)
    {
        //if property is string
        if(property.PropertyType == typeof(string))
        {
            string value = property.GetValue(entity, null) as string;
            if (string.IsNullOrEmpty(value))
                property.SetValue(entity, default, null);
        }
    }
    return entity;
}

And just call it before your insert like

your_object = SetNullStringToDefault<EntityType>(your_object, "Default Value");

Just pass it your object, it will convert all your NULL string properties to your default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM