locked
Getting non-nullable property in EntityFramework Entity to accept empty string RRS feed

  • Question

  • User1642115476 posted

    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); // used to be ...Default(null)
    ...
    }
    }

    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.

    Friday, December 23, 2016 12:38 AM

All replies

  • User-271186128 posted

    Hi gib9898_00,

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

    I think the problem is caused by your ICsvReader.

    I’m using CsvHelper, and I can get the value as empty string(“”), if I don’t set the value in csv file.

    You can use this, if you want to get empty string.

    My code is:

    using (var sr = new StreamReader(@"Book1.csv"))
                {               
                    var csv = new CsvReader(sr);
                    while (csv.Read())
                    {
                        var id = csv.GetField<string>(0);
                        var a = csv.GetField<string>(1);
                        var b = csv.GetField<string>(2);
                    }
                }
    

    And my data in csv is:

    Id,a,b

    1,2,3

    ,2,3

    I can see the empty string when I debug.

    Here is a link about CSVHelper: https://joshclose.github.io/CsvHelper/

    Best regards,
    Billy

     

    Monday, December 26, 2016 9:45 AM
  • User1642115476 posted

    I must apologize. The problem had nothing to do with the Bowtie entity or the CsvReader.

    Apparently, we had some code in our application that was nullifying the Bowties when the first field is blank. I didn't know it was there and the developer who wrote it no longer works here.

    It was this:

        public partial class Bowtie : IEmpty, IAuditable
        {
            public bool IsEmpty()
            {
                return string.IsNullOrWhiteSpace(Center);
            }
        }

    This method, which is a required override for IEmpty, returns false when Center is null or whitespace (the first field). I guess this was resulting in Bowtie being null. When I change it to:

        public partial class Bowtie : IEmpty, IAuditable
        {
            public bool IsEmpty()
            {
                return string.IsNullOrWhiteSpace(Center)
                    && string.IsNullOrWhiteSpace(BowtieCenterEquipment)
                    && (BowtieDMEquipmentLookupId == null || BowtieDMEquipmentLookupId == 0);
            }
        }

    I get the desired effect.

    Wednesday, December 28, 2016 3:44 PM