Hi @Kris Sapin ,
SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
I have reproduced the problem on my side, the issue relates the [Required]
attribute and the Allow Nulls
setting in the Database Product Designer.
If we are using the [Required]
attribute, after migration using EF core, the generate columns are not nullable. Then, if we change the Allow Nulls
setting (to allow the nullable value) via the Database Product Designer, and then store the null value in the Table, after that, when query the table it will show the above issue, like this:
So, to solve this issue, try to remove the [Required]
attribute from the Product class, like this:
public partial class Product
{
[Key]
public int ProductId { get; set; }
[StringLength(50)]
[DisplayName("Item Name")]
public string ItemName { get; set; }
[StringLength(1000)]
[DisplayName("Short Description")]
public string ShortDescription { get; set; }
[Column(TypeName = "decimal(18, 0)")]
[DisplayName("Price/PHP")]
public decimal? Price { get; set; }
[StringLength(1000)]
[DisplayName("Full Description")]
public string FullDescription { get; set; }
[StringLength(50)]
[DisplayName("Calibre")]
public string Calibre { get; set; }
[StringLength(50)]
[DisplayName("Movement")]
public string Movement { get; set; }
[StringLength(50)]
[DisplayName("Weight")]
public string Weight { get; set; }
[StringLength(50)]
[DisplayName("Height")]
public string Height { get; set; }
[StringLength(50)]
[DisplayName("Diameter")]
public string Diameter { get; set; }
[StringLength(50)]
[DisplayName("Thickness")]
public string Thickness { get; set; }
[StringLength(50)]
[DisplayName("Jewelries")]
public string Jewel { get; set; }
[StringLength(50)]
[DisplayName("Case Material")]
public string CaseMaterial { get; set; }
[StringLength(50)]
[DisplayName("Strap Material")]
public string StrapMaterial { get; set; }
}
Then, you can create a ProductViewModel with the [Required]
attribute, then use this view model to Insert/Edit the new product. [Note] by using this method, before insert the new item into the database or query the database, you need to convert the model between Product and ProductViewModel.
Otherwise, if you still using the [Required]
attribute in the Project class, you can unchecked the Allow Nulls
checkboxes in the Database Product Designer (set the columns not nullable), the update the database. After that view the table data via the SSMS, and find all null
value and then set its value. Then, you can query the table and get the data.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion