Home > OS >  fix " Data is Null. This method or property cannot be called on Null values " without usin
fix " Data is Null. This method or property cannot be called on Null values " without usin

Time:04-07

im trying to retrieve data form database using entity framework,i have table "Posts" which have many properties , one of these properties is "Image" , image property was not required before , so i have many stored records in "Posts" table with value null for "image", now i changed "image" to be required, now when try to retrieve old recoreds from "Posts" table asp.net launch this exception ("Data is Null. This method or property cannot be called on Null values").

what i need: i know that i can fix this problem by remove remove [required] annotation from "image" property, but i want to keep this data annotation and retrieve all recordes weather the "image" value is Null or not Null.

is there any way to do that ?

my code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace ARID.Models
{
public class Post
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "طول العنوان يجب ان يتراوح بين 25 حرف و 100 حرف", MinimumLength = 25)]
    [Display(Name ="العنوان")]
    public string Title { get; set; }

    [Required]
    [StringLength(5000, ErrorMessage = "يجب ألا يتجاوز المحتوى عن 900 كلمة ولايقل عن 50 كلمة", MinimumLength = 50)]
   
          [Display(Name = "المحتوى")]
    public string Body { get; set; }

    [Display(Name = "تاريخ النشر")]
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime DateTime { get; set; }

    [Display(Name = "السماح بالتعليقات")]
    public bool IsCommentsAllowed { get; set; }

    [StringLength(100)]
    [Display(Name = "الصورة")]
    [Required]
    public string Image { get; set; }

    [StringLength(100)]
    [Display(Name = "الملف المرفق")]
    public string File { get; set; }

    [Display(Name = "تمت الموافقة")]
    public bool IsApproved { get; set; }

    [Display(Name = "اخفاء المنشور")]
    public bool IsHidden { get; set; }

    [Display(Name = "مميز")]
    public bool IsFeatured { get; set; }

    [Display(Name = "هدية لافضل اجابة")]
    public bool IsGifted { get; set; }

    [Display(Name = "نوع الهدية")]
    public GiftType GiftType { get; set; }

    [Display(Name = "عدد القراء")]
    public int Reads { get; set; }

    [Display(Name = "حذف المنشور")]
    public bool IsDeleted { get; set; }

    [Display(Name = "المجتمع")]
    public int CommunityId { get; set; }
    [Display(Name = "المجتمع")]
    public  Community Community { get; set; }

    [Display(Name = "الناشر")]
    [StringLength(450)]
    public string ApplicationUserId { get; set; }
    [Display(Name = "الناشر")]
    public  ApplicationUser ApplicationUser { get; set; }

    [StringLength(100, ErrorMessage = "الكلمات المفتاحية قصيرة", MinimumLength = 5)]
    //[Required(ErrorMessage = "حقل الكلمات المفتاحية ضروري")]
    [Display(Name = "كلمات مفتاحية")]
    public string Tags { get; set; }

          [Display(Name = "نوع المنشور")]
    public GroupPostType PostType { get; set; }

    [Display(Name = "طلب النشر في مدونة منصة اريد")]
    public bool IsPublishRequest { get; set; }

    [Display(Name = "حالة طلب النشر")]
    public bool PublishRequestStatus { get; set; }

}

}

and in controller:

 public async Task<JsonResult> GetWallContents()
    {
        ARIDWallViewModel WallVM = new ARIDWallViewModel()
        {
            Posts = _context.Posts.Include(a => a.Community).Include(a => a.ApplicationUser).Where(a => a.Community.CommunityType == CommunityType.Personal && !a.IsHidden).OrderByDescending(a => a.DateTime).Take(25),
            PostComments = _context.PostComments.Include(a => a.ApplicationUser).Include(a => a.Post).OrderByDescending(a => a.DateTime).Take(25),
            Publications = _context.Publications.Include(a => a.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),

            Courses = _context.Courses.Include(a=>a.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),
            FreelancerReadyServices = _context.FreelancerReadyServices.Include(c => c.ApplicationUser).OrderByDescending(a => a.DateOfRecord).Take(25),
            Books = _context.Book.Include(c => c.ApplicationUser).Where(c => c.IsARIDPublications == true && c.IsFeatured == true).OrderByDescending(a => a.RecordDate).Take(25),
        };
        //ViewData["Count"] = _context.PostMetric.Count(a => a.PostId == id);
        return Json(WallVM);
    }

CodePudding user response:

The short answer is: No you can't.

In your database there are already entries which do not have the image property set and now you have changed it to being required, so your existing entries are not valid anymore according to the new rules. Therefore EF cannot retrieve them as it just follows the rules you set up for your model - additionally it would be bad coding style to have a property being marked as required (and non-nullable) and then in case of an existing entry still getting null (you would not even get compiler warnings for these null references as your string is marked as non nullable).

Therefore you must fix existing entries through a migration (you propably haven't created one after you added the required attribute because otherwise EF would have generated a migration which changes the column in the database to "non-nullable" which would fail because of existing rows containing null - thats why you should always generate migrations even after minor changes to the model). Just create a migration and set the Image property for existing entries to a default value.

If you do not want to modify existing entries then you propably have no other choice but to remove the required attribute from the Image property (as the property is not really required if some instances do not provide a value) and do the validation for new entries yourself by making the setter for Image private and adding an UpdateImage method which validates that the new image is not empty and sets the property.

  • Related