Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 若实体中的字符串为空,则在SQL中使用默认值_C#_Sql_Sql Server_Entity Framework - Fatal编程技术网

C# 若实体中的字符串为空,则在SQL中使用默认值

C# 若实体中的字符串为空,则在SQL中使用默认值,c#,sql,sql-server,entity-framework,C#,Sql,Sql Server,Entity Framework,我有一个数据库优先方法和SQL Server数据库的项目 在其中一个表中,我有一个不可为空的列,默认值为空字符串: [Comments] NVARCHAR(1024) NOT NULL DEFAULT ('') 此处的默认值应始终为空字符串,以完全支持odata“Is not empty”过滤器,否则该过滤器将返回具有空值的记录,从业务角度来看,这些记录实际上是空的 在我的模型中,此列还有一个字符串属性: public string Comments { get; set; } 现在发生的事

我有一个数据库优先方法和SQL Server数据库的项目

在其中一个表中,我有一个不可为空的列,默认值为空字符串:

[Comments] NVARCHAR(1024) NOT NULL DEFAULT ('')
此处的默认值应始终为空字符串,以完全支持odata“Is not empty”过滤器,否则该过滤器将返回具有空值的记录,从业务角度来看,这些记录实际上是空的

在我的模型中,此列还有一个字符串属性:

public string Comments { get; set; }
现在发生的事情是:如果适当的输入没有填充数据,我可能从前端得到空值,这个值将被映射到实体中,然后由EF插入

如果我只是尝试设置一个带有null“Comments”属性的记录,而不是在SQL端获取默认值,我会得到一个异常

我试图通过在EDMX中将此列的
StoreGeneratedPattern
设置为
Computed
来修复它。现在我可以插入带null的记录,并得到默认值,但每次都会得到,即使实体中有一个值

换句话说,computed
StoreGeneratedPattern
总是忽略实体中此属性的值,禁用
StoreGeneratedPattern
基本上使我在SQL端没有默认值

我想要的是以一种好的方式在后端解决这个问题

  • 我不想通过总是从前端发送空字符串并向后端添加此属性从不为null的验证来解决这个问题。这将迫使我对其他客户端(例如移动客户端)以及遇到相同情况的每个表单和表执行相同的操作

  • 我不想通过做以下事情来解决它:

    entity.Comments = entity.Comments ?? string.empty; 
    
    在后端。这将迫使我在我处理这个和其他类似实体的每个地方这样做

  • 我不想通过为类似的实体使用一些接口或者在构造函数中初始化这个属性来解决这个问题。我有一个T4模板,它从EDMX为我构建实体模型,以实现解耦,因此所有模型都是自动生成的,不应进行自定义

  • 我期望的行为和我希望达到的行为如下:

    如果EF尝试为该列插入一个空值的记录,SQL Server将使用默认值,否则将插入实体拥有的内容


    任何想法或解决方案都将不胜感激。

    我在CRUD函数中有一个用于此类任务的通用函数

    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;
    }
    
    private T SetNullStringToDefault(T实体,字符串默认值)
    {
    列表属性=entity.GetType().GetProperties().ToList();
    foreach(属性中的var属性)
    {
    //如果属性是字符串
    if(property.PropertyType==typeof(string))
    {
    string value=property.GetValue(实体,null)作为字符串;
    if(string.IsNullOrEmpty(value))
    SetValue(实体,默认,空);
    }
    }
    返回实体;
    }
    
    在插入之前调用它,就像

    your_object = SetNullStringToDefault<EntityType>(your_object, "Default Value");
    
    your_对象=SetNullStringToDefault(您的_对象,“默认值”);
    

    只要将其传递给对象,它就会将所有空字符串属性转换为默认值。

    我在CRUD函数中有一个用于此类任务的通用函数

    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;
    }
    
    private T SetNullStringToDefault(T实体,字符串默认值)
    {
    列表属性=entity.GetType().GetProperties().ToList();
    foreach(属性中的var属性)
    {
    //如果属性是字符串
    if(property.PropertyType==typeof(string))
    {
    string value=property.GetValue(实体,null)作为字符串;
    if(string.IsNullOrEmpty(value))
    SetValue(实体,默认,空);
    }
    }
    返回实体;
    }
    
    在插入之前调用它,就像

    your_object = SetNullStringToDefault<EntityType>(your_object, "Default Value");
    
    your_对象=SetNullStringToDefault(您的_对象,“默认值”);
    

    只需将其传递给对象,它就会将所有空字符串属性转换为默认值。

    在实体构造函数中将其设置为空字符串。在c中,您可以在实体构造函数中或使用属性初始值设定项在属性上设置值的默认值。您还可以在DbContext或ObjectContext中创建一个覆盖,以便将正在持久化的字符串属性的任何更改的值设置为空字符串(您甚至可以使用属性进行筛选)。或者在数据库中,您可以创建一个触发器,在值为null时将其更改为空字符串。我不知道你为什么关心OData,OData应该很好地处理空/空比较。如果有空字符串,最好将该值设置为null。@Igor我正在执行ODataQueryOptions到ODataQueryOptions的转换,ODataQueryOptions内部使用automapper和表达式映射。基本上,“不为空”过滤器在sql查询中结束;其中columnname!='';由于许多因素,仅使用空字符串直接初始化此属性不是解决方案。但我喜欢你关于扳机的主意。我试试看!在实体构造函数中将其设置为空字符串。在c#中,可以在实体构造函数中或使用属性初始值设定项在属性上设置默认值。您还可以在DbContext或ObjectContext中创建一个覆盖,以便将正在持久化的字符串属性的任何更改的值设置为空字符串(您甚至可以使用属性进行筛选)。或者在数据库中,您可以创建一个触发器,在值为null时将其更改为空字符串。我不知道你为什么关心OData,OData应该很好地处理空/空比较。如果有空字符串,最好将该值设置为null。@Igor我正在执行ODataQueryOptions到ODataQueryOptions的转换,ODataQueryOptions内部使用automapper和表达式映射。基本上,“不为空”过滤器在sql查询中结束;其中columnname!='';由于许多因素,仅使用空字符串直接初始化此属性不是解决方案。但是我喜欢你的我