Home > Blockchain >  My table contains nullable value that should not be null
My table contains nullable value that should not be null

Time:11-04

I'm trying to seed data to my database. I have classes Photos and AppUser. They have a one to many relation of ICollection<Photo> Photos property, which means every user can can have multiple images.

AppUser class :

 public class AppUser
 {
 public int Id { get; set; }
 public string UserName { get; set; }
 public byte[] PasswordHash { get; set; }
 public byte[] PasswordSalt { get; set; }
 public DateTime DateOfBirth { get; set; }
 public string KnownAs { get; set; }
 public DateTime Created { get; set; } = DateTime.Now;
 public DateTime LastSeen { get; set; } = DateTime.Now;
 public string Gender { get; set; }
 public string Introduction { get; set; }
 public string LookingFor { get; set; }
 public string Interests { get; set; }
 public string City { get; set; }
 public string Country { get; set; }
 public ICollection<Photo> Photos { get; set; }
}

Photo class :

namespace API.Entity
{
[Table("Photos")]
public class Photo
{
 public int Id { get; set; }
 public string UrL { get; set; }
 public bool IsMain { get; set; }
 public string PublicId { get; set; }
 public AppUser AppUser { get; set; }
 public int AppUserId { get; set; }
}
}

Seed class :

 public class Seed
 {
 public static async Task SeedUsers(DataContext context)
 {
 if (await context.Users.AnyAsync()) return;
 var userData = await System.IO.File.ReadAllTextAsync("Data/UserSeedData.json");
 var users = JsonSerializer.Deserialize<List<AppUser>>(userData);
 if (users == null) return;
 foreach (var user in users)
 {
   using var hmac = new HMACSHA512();
   user.UserName = user.UserName.ToLower();
   user.PasswordSalt = hmac.Key;
   user.PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes("pa$$w0rd"));

  await context.Users.AddAsync(user);
  }
  await context.SaveChangesAsync();
  }
 }

JSON file that generates seeding data (I have many, this is one of them) :

 [
 {
 "UserName": "Sandy",
 "Gender": "female",
 "DateOfBirth": "1978-02-23",
 "KnownAs": "Sandy",
 "Created": "2019-07-28",
 "LastActive": "2020-05-24",
 "Introduction": "Cupidatat do nostrud et culpa commodo enim minim quis. 
 "Interests": "Esse sunt sit fugiat tempor voluptate cillum mollit aliquip irure ipsum mollit quis",
 "City": "Moscow",
 "Country": "Denmark",

   "Photos": [
      {

      "Url": "https://randomuser.me/api/portraits/women/42.jpg",
      "IsMain": true
     }

    ]
    }
    ]

URL in Photo table is set to null. Despite of "IsMain" property is set it value and working fine. I tried to remove migration and start over but still null. I cant figure it out.

CodePudding user response:

There is a missmatch in Entity name and JSON object name for Url.

In your json file object name is : Url

But in your Entity class it's : UrL

Also, use List insted of ICollection.

AppUser Class :

public class AppUser
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public byte[] PasswordHash { get; set; }
    public byte[] PasswordSalt { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string KnownAs { get; set; }
    public DateTime Created { get; set; } = DateTime.Now;
    public DateTime LastSeen { get; set; } = DateTime.Now;
    public string Gender { get; set; }
    public string Introduction { get; set; }
    public string LookingFor { get; set; }
    public string Interests { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public List<Photo> Photos { get; set; }
}

Photo Class :

public class Photo
{
    public int Id { get; set; }
    public string Url { get; set; }
    public bool IsMain { get; set; }
    public string PublicId { get; set; }
    public AppUser AppUser { get; set; }
    public int AppUserId { get; set; }
}

JSON File :

[
  {
    "UserName": "Sandy",
    "Gender": "female",
    "DateOfBirth": "1978-02-23",
    "KnownAs": "Sandy",
    "Created": "2019-07-28",
    "LastActive": "2020-05-24",
    "Introduction": "Cupidatat do nostrud et culpa commodo enim minim quis.",
    "Interests": "Esse sunt sit fugiat tempor voluptate cillum mollit aliquip irure ipsum mollit quis",
    "City": "Moscow",
    "Country": "Denmark",
    "Photos": [
      {
        "Url": "https://randomuser.me/api/portraits/women/42.jpg",
        "IsMain": true
      }
    ]
  }
]
  • Related