我正在嘗試將資料播種到我的資料庫中。我有課Photos和AppUser. 它們具有一對多的ICollection<Photo> Photos屬性關系,這意味著每個用戶都可以擁有多個影像。
AppUser班級 :
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班級 :
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班級 :
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 檔案(我有很多,這是其中之一):
[
{
"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
}
]
}
]
表中的URLPhoto設定為空。盡管“IsMain”屬性已設定為值并且作業正常。我試圖洗掉遷移并重新開始,但仍然為空。我想不通。
uj5u.com熱心網友回復:
Url 的物體名稱和 JSON 物件名稱不匹配。
在您的 json 檔案中,物件名稱是:Url
但是在您的 Entity 類中,它是:UrL
此外,使用 ICollection 的 List insted。
應用用戶類:
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; }
}
照片類:
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檔案:
[
{
"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
}
]
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527247.html
標籤:C#。网sqlite
上一篇:需要提升權限的檔案
下一篇:SQL希望高效查詢SELECTcolumnA,columnBFROMtable1WHEREcolumnA,columnBareincolumnCFROMtable2WHEREcolumndD>v
