#nullable enable causes SqlNullValueException

A build warning message caused me to try adding the #nullable enable directive to my data entity class.

However I found this causes an SqlNullValueException when the following code runs.

var customerId = 5 // a valid key
var db = GetMyDbContext() // my code to get the DbContext
var cust = db.MyExtCustomers.AsNoTracking().SingleOrDefault(x => x.CustomerID == customerId);

The exception message is

System.Data.SqlTypes.SqlNullValueException: ‘Data is Null. This method
or property cannot be called on Null values.’

The call stack is

    Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.SqlDataReader.GetString(int i) Line 2122  C#
    [Lightweight Function]  
    Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<Entities.MYEXTCustomer>.Enumerator.MoveNext()    Unknown
    System.Linq.dll!System.Linq.Enumerable.TryGetSingle<Entities.MYEXTCustomer>(System.Collections.Generic.IEnumerable<Entities.MYEXTCustomer> source, out bool found) Line 7391    C#
    [Lightweight Function]  
    Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<Entities.MYEXTCustomer>(System.Linq.Expressions.Expression query)  Unknown
    Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<Entities.MYEXTCustomer>(System.Linq.Expressions.Expression expression)   Unknown
    dll!Helpers.DataAccessStd2.MakeReadOnlyProperties(string connectionString, int jobId) Line 439  C#

Is this error by design? How should I tackle refactoring my class?

0

So I originally assumed that it was just a data problem, and your data was null… but I think it’s because EF Core is lazily loading the data behind the scenes. See the EF Core docs for more info.

Basically when you do that .String it actually goes to look up the data for the first time, so the problem isn’t in that call. It’s in the data lookup.

To get it working properly, you need to change the customer lookup and use the null-forgiving operator. Here’s the example EF Core gives:

var order = context.Orders
    .Where(o => o.OptionalInfo!.SomeProperty == "foo")
    .ToList();

Translating it to your code, I think this might work for you:

var cust = db.MyExtCustomers.AsNoTracking().SingleOrDefault(x => x!.CustomerID == customerId);

Note the extra null-forgiving operator ! after the x.

I think this can only happen though if you’ve got some relationships here which you’re not showing as part of this code (ie: x!.CustomerId is really x.Thing!.CustomerId) and Thing is sometimes null.

If that’s not the case, the data is actually null and something is up with your call. Try adding a .ToList() to your customer look-up to force it to load, then you can see more clearly what the results of that query are.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

#nullable enable causes SqlNullValueException

A build warning message caused me to try adding the #nullable enable directive to my data entity class.

However I found this causes an SqlNullValueException when the following code runs.

var customerId = 5 // a valid key
var db = GetMyDbContext() // my code to get the DbContext
var cust = db.MyExtCustomers.AsNoTracking().SingleOrDefault(x => x.CustomerID == customerId);

The exception message is

System.Data.SqlTypes.SqlNullValueException: ‘Data is Null. This method
or property cannot be called on Null values.’

The call stack is

    Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.SqlDataReader.GetString(int i) Line 2122  C#
    [Lightweight Function]  
    Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<Entities.MYEXTCustomer>.Enumerator.MoveNext()    Unknown
    System.Linq.dll!System.Linq.Enumerable.TryGetSingle<Entities.MYEXTCustomer>(System.Collections.Generic.IEnumerable<Entities.MYEXTCustomer> source, out bool found) Line 7391    C#
    [Lightweight Function]  
    Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<Entities.MYEXTCustomer>(System.Linq.Expressions.Expression query)  Unknown
    Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<Entities.MYEXTCustomer>(System.Linq.Expressions.Expression expression)   Unknown
    dll!Helpers.DataAccessStd2.MakeReadOnlyProperties(string connectionString, int jobId) Line 439  C#

Is this error by design? How should I tackle refactoring my class?

0

So I originally assumed that it was just a data problem, and your data was null… but I think it’s because EF Core is lazily loading the data behind the scenes. See the EF Core docs for more info.

Basically when you do that .String it actually goes to look up the data for the first time, so the problem isn’t in that call. It’s in the data lookup.

To get it working properly, you need to change the customer lookup and use the null-forgiving operator. Here’s the example EF Core gives:

var order = context.Orders
    .Where(o => o.OptionalInfo!.SomeProperty == "foo")
    .ToList();

Translating it to your code, I think this might work for you:

var cust = db.MyExtCustomers.AsNoTracking().SingleOrDefault(x => x!.CustomerID == customerId);

Note the extra null-forgiving operator ! after the x.

I think this can only happen though if you’ve got some relationships here which you’re not showing as part of this code (ie: x!.CustomerId is really x.Thing!.CustomerId) and Thing is sometimes null.

If that’s not the case, the data is actually null and something is up with your call. Try adding a .ToList() to your customer look-up to force it to load, then you can see more clearly what the results of that query are.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật