EF:NotMapped 的不可为 null 的 UpdateException 属性
EF: Non-nullable UpdateException for NotMapped property
我在 ASP.NET 页面上定期同步数据。我有一个包含两个复杂成员的对象,我在 Entity Framework 中使用 NotMapped 属性忽略了它们。相反,我存储序列化值。
型号:
public class Game
{
//...
[NotMapped]
public Image Image { get; set; }
[NotMapped]
public List<Image> Images { get; set; }
public string Image_Serialized
{
get
{
return JsonConvert.SerializeObject(Image);
}
set
{
Image = JsonConvert.DeserializeObject<Image>(value);
}
}
public string Images_Serialized
{
get
{
return JsonConvert.SerializeObject(Images);
}
set
{
Images = JsonConvert.DeserializeObject<List<Image>>(value);
}
}
//...
}
还有……
public class Image
{
public string IconUrl { get; set; }
public string MediumUrl { get; set; }
public string ScreenUrl { get; set; }
public string SmallUrl { get; set; }
public string SuperUrl { get; set; }
public string ThumbUrl { get; set; }
public string TinyUrl { get; set; }
}
同步完成后,我更新数据库:
foreach(var game in games)
{
// address any null complex types.
if (game.Image == null) game.Image = new Image();
if (game.Images == null) game.Images = new List<Image>();
// add game if new, update if already in db
var dbGame = db.Games.Where(g => g.Id == game.Id).FirstOrDefault();
if (dbGame == null)
{
db.Games.Add(game);
}
else
{
var queriedGame = db.Entry(dbGame);
queriedGame.CurrentValues.SetValues(game);
queriedGame.State = System.Data.Entity.EntityState.Modified;
}
}
// returns 0 results... seems fine
var badGames = games.Where(g => g.Image == null || g.Images == null).ToList();
db.SaveChanges();
我在 db.SaveChanges() 收到以下异常:
Server Error in '/' Application.
Null value for non-nullable member. Member: 'Image'.
感谢您的帮助。此外,如果我不希望它们的值直接存储在数据库中(NotMapped 属性),为什么我需要担心这些值是否为 null?
您可能需要使用图像?而不是图像作为数据类型。
我不建议您使用这样的模型,而是在您的游戏中使用图像 ID class 和 ICollection
个图像(事实上,为什么两者都有)。
话虽如此,您可以尝试使用当前模型
public class Game
{
//...
[NotMapped]
public Image? Image { get; set; } //<== see ? to make it nullable
[NotMapped]
public List<Image> Images { get; set; }
public string Image_Serialized
{
get
{
if(Image == null)
return null;
return JsonConvert.SerializeObject(Image);
}
set
{
if(value == null)
Image = null;
else
Image = JsonConvert.DeserializeObject<Image>(value);
}
}
}
+1 给阿里,让我走上正确的方向。
将图像更改为结构并使图像引用可为空后,我仍然遇到相同的错误。问题在于 Game 对象引用了许多其他对象类型(开发者、发布者、发布者等)。其中一些对象中有图像引用,我尚未将其设置为 NotMapped。我的假设是,当我调用 db.SaveChanges() 时,它试图向这些数据表中添加条目,并导致出现问题。
我在 ASP.NET 页面上定期同步数据。我有一个包含两个复杂成员的对象,我在 Entity Framework 中使用 NotMapped 属性忽略了它们。相反,我存储序列化值。
型号:
public class Game
{
//...
[NotMapped]
public Image Image { get; set; }
[NotMapped]
public List<Image> Images { get; set; }
public string Image_Serialized
{
get
{
return JsonConvert.SerializeObject(Image);
}
set
{
Image = JsonConvert.DeserializeObject<Image>(value);
}
}
public string Images_Serialized
{
get
{
return JsonConvert.SerializeObject(Images);
}
set
{
Images = JsonConvert.DeserializeObject<List<Image>>(value);
}
}
//...
}
还有……
public class Image
{
public string IconUrl { get; set; }
public string MediumUrl { get; set; }
public string ScreenUrl { get; set; }
public string SmallUrl { get; set; }
public string SuperUrl { get; set; }
public string ThumbUrl { get; set; }
public string TinyUrl { get; set; }
}
同步完成后,我更新数据库:
foreach(var game in games)
{
// address any null complex types.
if (game.Image == null) game.Image = new Image();
if (game.Images == null) game.Images = new List<Image>();
// add game if new, update if already in db
var dbGame = db.Games.Where(g => g.Id == game.Id).FirstOrDefault();
if (dbGame == null)
{
db.Games.Add(game);
}
else
{
var queriedGame = db.Entry(dbGame);
queriedGame.CurrentValues.SetValues(game);
queriedGame.State = System.Data.Entity.EntityState.Modified;
}
}
// returns 0 results... seems fine
var badGames = games.Where(g => g.Image == null || g.Images == null).ToList();
db.SaveChanges();
我在 db.SaveChanges() 收到以下异常:
Server Error in '/' Application.
Null value for non-nullable member. Member: 'Image'.
感谢您的帮助。此外,如果我不希望它们的值直接存储在数据库中(NotMapped 属性),为什么我需要担心这些值是否为 null?
您可能需要使用图像?而不是图像作为数据类型。
我不建议您使用这样的模型,而是在您的游戏中使用图像 ID class 和 ICollection
个图像(事实上,为什么两者都有)。
话虽如此,您可以尝试使用当前模型
public class Game
{
//...
[NotMapped]
public Image? Image { get; set; } //<== see ? to make it nullable
[NotMapped]
public List<Image> Images { get; set; }
public string Image_Serialized
{
get
{
if(Image == null)
return null;
return JsonConvert.SerializeObject(Image);
}
set
{
if(value == null)
Image = null;
else
Image = JsonConvert.DeserializeObject<Image>(value);
}
}
}
+1 给阿里,让我走上正确的方向。
将图像更改为结构并使图像引用可为空后,我仍然遇到相同的错误。问题在于 Game 对象引用了许多其他对象类型(开发者、发布者、发布者等)。其中一些对象中有图像引用,我尚未将其设置为 NotMapped。我的假设是,当我调用 db.SaveChanges() 时,它试图向这些数据表中添加条目,并导致出现问题。