Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.Net 8 Entity Framework returns null data when there is data in the table

In the lines below, the first line always returns a count and does it for every table.

The second line returns rows for some tables but null data for others (even ones with data and a count returned). The models are all correct and EF and ApplicationDbContext are set up correctly.

I can see nothing else obvious here, can anyone point out what it might be?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var count = _context.RequestLine.Count();
var query = await  _context.RequestLine.ToListAsync();

Error returned:
Data is Null. This method or property cannot be called on Null values.

[Table("REQUEST_LINES")]
public class RequestLine
{
    [Key]
    [MaxLength(30)]
    public string LineLookup { get; set; }  //DR01-1234567-10001

    [MaxLength(30)]
    public string Lookup { get; set; } //DR01-1234567

    [MaxLength(12)]
    public string DocumentId { get; set; }

    [MaxLength(80)]
    public string DocumentType { get; set; }

    [MaxLength(256)]
    public string DocumentDescription { get; set; }

    public DateTimeOffset Created { get; set; }

    public DateTimeOffset Modified { get; set; }
}

enter image description here

>Solution :

The problem may result in a conflict between the model in the project and the structure in the table from the database. In this case it may be that in one of the tables that you are trying to fetch the data, one of the columns may be nullable meanwhile in the model it is represented as a non nullable attribute.

For example, in your table you may have a bit Column1 that has a nullable property and this same column has null data in the records of the database. Meanwhile in the model you may have bool Column1, which it won’t accept any null data when retrieving the records required.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading