在EntityFramework Entity中获取不可为空的属性以接受空字符串 [英] Getting non-nullable property in EntityFramework Entity to accept empty string

查看:320
本文介绍了在EntityFramework Entity中获取不可为空的属性以接受空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我遇到的情况是我正在处理EntityFramework和实体中设置为不允许空值的字段。我有一个ICsvReader从CSV文件中读取值,这些值用于初始化实体。当它在CSV中遇到BowtieCenter字段的
空白值时,在将其分配给实体中的非可空属性BowtieCenter时,会将其转换为null。结果是读者返回的实体为空。



这在所有情况下都没问题,但现在我们要实现一些要求我们将该属性设置为非空值,而不是空字符串。我们想要这样做的原因是,在某些条件下,我们希望向用户发出一个警告:
,CSV字段为空,应设置为某些内容。条件是:1)第二个字段(称为BowtieEquipment)被设置为某个东西,或者2)第三个字段(称为DataMiningEquipment)被设置为某个东西。如果满足这些条件中的任何一个或两个,
用户需要用一些东西填写BowtieCenter。



问题是当BowtieCenter空白时,读者为我的实体返回null,这意味着我甚至无法检查属性BowtieEquipment或DataMiningEquipment是否设置为某个东西。



我尝试了几件事,既不是工作:



1)我尝试将BowtieCenter属性的默认值设置为String.Empty:



  &NBSP;公共类BowtieMap:CsvClassMap< Bowtie>

  &NBSP; {

  &NBSP; &NBSP; &NBSP;公共BowtieMap()

  &NBSP; &NBSP; &NBSP; {

...

地图(b => b.Center).Index(6 ).Default(string.Empty);

...

}

  &NBSP; }


但实体(Bowtie)仍然返回null。



2)我尝试创建一个解析价值的特殊功能:



  &NBSP;公共类BowtieMap:CsvClassMap< Bowtie>

  &NBSP; {

  &NBSP; &NBSP; &NBSP;公共BowtieMap()

  &NBSP; &NBSP; &NBSP; {

...

地图(b => b.Center).ConvertUsing(x => x.ParseBowtieCenter(6));

...

}

  &NBSP; }


...



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return x.GetField(fieldIndex).Trim();

  &NBSP; &NBSP; &NBSP; }


但我仍然是空的。



我甚至尝试发回非空字符串但是还是空白:



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

    var field = x.GetField(fieldIndex).Trim();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return field == string.Empty? " " :field;

  &NBSP; &NBSP; &NBSP; }


但仍然为空。



最后,我试过这个:



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

    var field = x.GetField(fieldIndex).Trim();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return field == string.Empty? " _" :field;

  &NBSP; &NBSP; &NBSP; }


这是唯一可行的。显然,BowtieCenter返回的值根本不能是空格。



为什么不呢?为什么EntityFramework坚持要对待" &NBSP; &NBSP; &NBSP;   / t / t / t  &NBSP; &NBSP; &NBSP;   / n / n  / r / r  &NBSP;"与null没什么不同?

$
有没有办法让它允许空格作为可接受的非空值?最好是空字符串?



返回一个非空白字符串对我来说似乎不好编程。



不,在数据库或实体中允许null不是一个选项(在我检查BowtieEquipment和DataMiningEquipment的值之后,如果需要,我会将Bowtie Entity设置为null)。



感谢您的帮助。

Hello,

I have a situation in which I'm dealing with EntityFramework and a field in an Entity that is set to not allow nulls. I have an ICsvReader that's reading values from a CSV file, and these values are being used to initialize the Entity. When it encounters a blank value for the field BowtieCenter in the CSV, it converts that to null when assigning it to the non-nullable property BowtieCenter in the Entity. The result is that the Entity the reader returns is null.

This was fine in all case, but now we want to implement something that requires us to set that property to a non-null value, like an empty string instead. The reason we want to do this is that, under certain condtions, we want to give the user a warning that the CSV field is blank and should be set to something. The conditions are: 1) that a second field (called BowtieEquipment) is set to something, or 2) that a third (called DataMiningEquipment) is set to something. If either or both of these conditions are met, the user needs to fill in BowtieCenter with something.

The problem is that when BowtieCenter is blank, the reader returns null for my Entity, which means I can't even check if the properties BowtieEquipment or DataMiningEquipment are set to something.

I tried a couple things, neither of which worked:

1) I tried setting the default value for the BowtieCenter property to String.Empty:

    public class BowtieMap : CsvClassMap<Bowtie>
    {
        public BowtieMap()
        {
...
Map(b => b.Center).Index(6).Default(string.Empty);
...
}
    }

But the Entity (Bowtie) still comes back null.

2) I tried creating a special function that parses the value:

    public class BowtieMap : CsvClassMap<Bowtie>
    {
        public BowtieMap()
        {
...
Map(b => b.Center).ConvertUsing(x => x.ParseBowtieCenter(6));
...
}
    }

...

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
            return x.GetField(fieldIndex).Trim();
        }

But still I get null.

I even tried sending back a non-empty string but still whitespace:

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
   var field = x.GetField(fieldIndex).Trim();

            return field == string.Empty ? " " : field;
        }

But still null.

Finally, I tried this:

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
   var field = x.GetField(fieldIndex).Trim();

            return field == string.Empty ? "_" : field;
        }

This is the only thing that works. Apparently, the value returned for BowtieCenter can't be whitespace at all.

Why not? Why does EntityFramework insist on treating "        /t/t/t          /n/n   /r/r    " as no different from null?

Is there a way to GET it to allow whitespace as an acceptable non-null value? Preferably empty string?

Returning a non-whitespace string just seems like bad programming to me.

And no, allowing for null in the database or the Entity is not an option (after I check the values of BowtieEquipment and DataMiningEquipment, I will set the Bowtie Entity to null if necessary).

Thanks for your help.

推荐答案

你好,



我有一个情况我正在处理EntityFramework和实体中设置为不允许空值的字段。我有一个ICsvReader从CSV文件中读取值,这些值用于初始化实体。当它在CSV中遇到BowtieCenter字段的
空白值时,在将其分配给实体中的非可空属性BowtieCenter时,会将其转换为null。结果是读者返回的实体为空。



这在所有情况下都没问题,但现在我们要实现一些要求我们将该属性设置为非空值,而不是空字符串。我们想要这样做的原因是,在某些条件下,我们希望向用户发出一个警告:
,CSV字段为空,应设置为某些内容。条件是:1)第二个字段(称为BowtieEquipment)被设置为某个东西,或者2)第三个字段(称为DataMiningEquipment)被设置为某个东西。如果满足这些条件中的任何一个或两个,
用户需要用一些东西填写BowtieCenter。



问题是当BowtieCenter空白时,读者为我的实体返回null,这意味着我甚至无法检查属性BowtieEquipment或DataMiningEquipment是否设置为某个东西。



我尝试了几件事,既不是工作:



1)我尝试将BowtieCenter属性的默认值设置为String.Empty:



  &NBSP;公共类BowtieMap:CsvClassMap< Bowtie>

  &NBSP; {

  &NBSP; &NBSP; &NBSP;公共BowtieMap()

  &NBSP; &NBSP; &NBSP; {

...

地图(b => b.Center).Index(6 ).Default(string.Empty);

...

}

  &NBSP; }


但实体(Bowtie)仍然返回null。



2)我尝试创建一个解析价值的特殊功能:



  &NBSP;公共类BowtieMap:CsvClassMap< Bowtie>

  &NBSP; {

  &NBSP; &NBSP; &NBSP;公共BowtieMap()

  &NBSP; &NBSP; &NBSP; {

...

地图(b => b.Center).ConvertUsing(x => x.ParseBowtieCenter(6));

...

}

  &NBSP; }


...



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return x.GetField(fieldIndex).Trim();

  &NBSP; &NBSP; &NBSP; }


但我仍然是空的。



我甚至尝试发回非空字符串但是还是空白:



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

    var field = x.GetField(fieldIndex).Trim();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return field == string.Empty? " " :field;

  &NBSP; &NBSP; &NBSP; }


但仍然为空。



最后,我试过这个:



  &NBSP; &NBSP; &NBSP; public static string ParseBowtieCenter(此ICsvReaderRow x,int fieldIndex)

  &NBSP; &NBSP; &NBSP; {

    var field = x.GetField(fieldIndex).Trim();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return field == string.Empty? " _" :field;

  &NBSP; &NBSP; &NBSP; }


这是唯一可行的。显然,BowtieCenter返回的值根本不能是空格。



为什么不呢?为什么EntityFramework坚持要对待" &NBSP; &NBSP; &NBSP;   / t / t / t  &NBSP; &NBSP; &NBSP;   / n / n  / r / r  &NBSP;"与null没什么不同?

$
有没有办法让它允许空格作为可接受的非空值?最好是空字符串?



返回一个非空白字符串对我来说似乎不好编程。



不,在数据库或实体中允许null不是一个选项(在我检查BowtieEquipment和DataMiningEquipment的值之后,如果需要,我会将Bowtie Entity设置为null)。



感谢您的帮助。
Hello,

I have a situation in which I'm dealing with EntityFramework and a field in an Entity that is set to not allow nulls. I have an ICsvReader that's reading values from a CSV file, and these values are being used to initialize the Entity. When it encounters a blank value for the field BowtieCenter in the CSV, it converts that to null when assigning it to the non-nullable property BowtieCenter in the Entity. The result is that the Entity the reader returns is null.

This was fine in all case, but now we want to implement something that requires us to set that property to a non-null value, like an empty string instead. The reason we want to do this is that, under certain condtions, we want to give the user a warning that the CSV field is blank and should be set to something. The conditions are: 1) that a second field (called BowtieEquipment) is set to something, or 2) that a third (called DataMiningEquipment) is set to something. If either or both of these conditions are met, the user needs to fill in BowtieCenter with something.

The problem is that when BowtieCenter is blank, the reader returns null for my Entity, which means I can't even check if the properties BowtieEquipment or DataMiningEquipment are set to something.

I tried a couple things, neither of which worked:

1) I tried setting the default value for the BowtieCenter property to String.Empty:

    public class BowtieMap : CsvClassMap<Bowtie>
    {
        public BowtieMap()
        {
...
Map(b => b.Center).Index(6).Default(string.Empty);
...
}
    }

But the Entity (Bowtie) still comes back null.

2) I tried creating a special function that parses the value:

    public class BowtieMap : CsvClassMap<Bowtie>
    {
        public BowtieMap()
        {
...
Map(b => b.Center).ConvertUsing(x => x.ParseBowtieCenter(6));
...
}
    }

...

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
            return x.GetField(fieldIndex).Trim();
        }

But still I get null.

I even tried sending back a non-empty string but still whitespace:

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
   var field = x.GetField(fieldIndex).Trim();

            return field == string.Empty ? " " : field;
        }

But still null.

Finally, I tried this:

        public static string ParseBowtieCenter(this ICsvReaderRow x, int fieldIndex)
        {
   var field = x.GetField(fieldIndex).Trim();

            return field == string.Empty ? "_" : field;
        }

This is the only thing that works. Apparently, the value returned for BowtieCenter can't be whitespace at all.

Why not? Why does EntityFramework insist on treating "        /t/t/t          /n/n   /r/r    " as no different from null?

Is there a way to GET it to allow whitespace as an acceptable non-null value? Preferably empty string?

Returning a non-whitespace string just seems like bad programming to me.

And no, allowing for null in the database or the Entity is not an option (after I check the values of BowtieEquipment and DataMiningEquipment, I will set the Bowtie Entity to null if necessary).

Thanks for your help.


这篇关于在EntityFramework Entity中获取不可为空的属性以接受空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆