Home > Mobile >  Retrieve nullable object using EF CORE
Retrieve nullable object using EF CORE

Time:12-09

I am a beginner, and while testing some code, I can't seem to understand how to do this properly..

1st: I have a City class:

public class City
{
    public City()
    {
        ZipCode = "";
        Name = "";
    }

    public int Id { get; set; }
    public string ZipCode { get; set; }
    public string Name { get; set; }
}

2nd: I have a Contact class that uses a nullable City class (in case the user does not know the city):

public class Contact
{
    public Contact()
    {
        Name = "";
        Line1 = "";
        Line2 = "";
        CityId = null;
        City = new City();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }

    private int? _CityId;
    public int? CityId
    {
        get { return _CityId; }
        set { _CityId = value < 1 ? null : value; }
    }

    private City _City;
    public City City
    {
        get { return _City; }
        set { _City = _CityId == null ? null : value; }
    }
}

The problem I encounter is when I retrieve a record that has stored a null City (when retrieving a record and its City is not null, everything will work fine). My .Select() statement looks like this:

var result = await _context.Contact
            .Where(w => w.Id == id)
            .Include(q => q.City)
            .Select(s => new Contact
            {
                // Entity
                Id = s.Id,
                // Model
                Line1 = s.Line1,
                Line2 = s.Line2,
                CityId = s.CityId,
                City = new City // I am retrieving this so that I can get the inner data as well
                {
                    // Entity
                    Id = s.City.Id,
                    // Model
                    ZipCode = s.City.ZipCode,
                    Name = s.City.Name,
                }
            }).FirstOrDefaultAsync();

The output is fine for records that does not have a null City, but if user retrieves a record with null City, it throws the following error:

Nullable object must have a value.

Can anybody please teach me how to do this properly? Thank you in advance!

CodePudding user response:

You don't need to create a new Entity using Select, you are getting the error because if s.City is null s.City.Id doesn't exists. Insteat search it directly using

var result = await _context.Contact
            .Include(q => q.City)
            .FirstOrDefaultAsync(x => x.Id == id);

CodePudding user response:

Why you use Select and using private property for the city?

Contact Class :

public class Contact
{
    public Contact()
    {
        Name = "";
        Line1 = "";
        Line2 = "";
        CityId = null;
        City = new City();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }

    public int? CityId;
    public City City
    
}

Your Select is the same as the entity class, and you don't need to use it.

var result = await _context.Contact
            .Include(q => q.City)
            .FirstOrDefaultAsync(f => f.Id == id);

CodePudding user response:

Thanks for the answers everyone!

I found that the culprit is this line of code (thanks to your answers and explanations):

Id = s.City.Id

That is because Id here is an int (not nullable since this is an Identity in MSSQL) but when CityId (and also City object) stores a null in the database, s.City.Id will also contain null in the .Select().

The Id = s.City.Id in the above case fails because Id (not nullable) is being forced to receive a null (contained in the s.City.Id).

  • Related