C# 使用.Include()EF Coe更新问题
我一直在关注EF core中并发冲突的处理。我有两种型号: Movie.csC# 使用.Include()EF Coe更新问题,c#,entity-framework-core,razor-pages,C#,Entity Framework Core,Razor Pages,我一直在关注EF core中并发冲突的处理。我有两种型号: Movie.cs public class Movie { [Key] public int ID { get; set; } [Required] [StringLength(60, MinimumLength = 3)] public string Title { get; set; } [DataType(DataType.Date)] [Display(Name = &
public class Movie
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(60, MinimumLength = 3)]
public string Title { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Release Date")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
[Range(1, 300)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
[Required]
[StringLength(5)]
[RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
public string Rating { get; set; }
public string Description { get; set; }
[Timestamp]
public byte[] ConcurrencyToken { get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
public override string ToString()
{
return Title;
}
}
public class Genre
{
public Genre()
{
Movies = new List<Movie>();
}
[Key]
public int GenreId { get; set; }
[Required]
[StringLength(30, MinimumLength = 5)]
[Display(Name = "Genre Title")]
[RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
public string GenreTitle { get; set; }
[Timestamp]
public byte[] ConcurrencyToken { get; set; }
[Display(Name = "Number of movies")]
public ICollection<Movie> Movies { get; set; }
public override string ToString()
{
return GenreTitle;
}
}
体裁.cs
public class Movie
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(60, MinimumLength = 3)]
public string Title { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Release Date")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime ReleaseDate { get; set; }
[Range(1, 300)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
[Required]
[StringLength(5)]
[RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
public string Rating { get; set; }
public string Description { get; set; }
[Timestamp]
public byte[] ConcurrencyToken { get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
public override string ToString()
{
return Title;
}
}
public class Genre
{
public Genre()
{
Movies = new List<Movie>();
}
[Key]
public int GenreId { get; set; }
[Required]
[StringLength(30, MinimumLength = 5)]
[Display(Name = "Genre Title")]
[RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
public string GenreTitle { get; set; }
[Timestamp]
public byte[] ConcurrencyToken { get; set; }
[Display(Name = "Number of movies")]
public ICollection<Movie> Movies { get; set; }
public override string ToString()
{
return GenreTitle;
}
}
更新在tryupdatemodelsync
行失败,并且没有转到try-catch
。我发现导致问题的错误是对电影
中包含的Genre.GenreTitle
属性的验证。即使ModelState没有失败,它仍然会导致更新实体的错误。然而,当我检查Movie.Genre属性时,它完全正常,不应该有任何错误。只有当我从var movieToUpdate=wait\u context.Movie.FirstOrDefaultAsync(id)
中删除.Include时,它才起作用
现在,我想知道我的错误是什么,因为在文档中嵌套的对象实际上是.include
我测试了您的正则表达式
^[A-Z]+[A-zA-Z]*$
,它似乎不接受空格字符。因此,体裁标题
与表达式不匹配并触发错误
您可以在
[]
中添加空格,告诉表达式您还需要接受空格字符。我不知道这是不是应该这样做,但它在或中起作用(最后一个没有c,但我认为正则表达式是通用的)。但是当我尝试更新我的流派实体时,它允许流派.Title
本身的空格。即使只有一个单词的标题也会出现错误。。。所以我想可能是别的。但是,我将尝试删除我的regex
,然后再试一次,谢谢!顺便说一句,当您在tryupdatemodelsync
方法中指定字段时,EF不应该忽略其中未指定的字段吗?因为Genre
嵌套在Movie
中,EF甚至不应该打扰他,因为它没有做任何更改,而且TryUpdate
@Miraziz>中的列列表中也没有提到它,但我不认为它可以完全避免未指定的属性。因为我们讨论的是模型,所以可能在某个地方有一个实例,所以可以读取属性,包括它的属性。不确定,但这就是我在这里的看法,如果我错了,也许有人会纠正我。那么,在传递新对象的同时手动指定属性就没有意义了。据我所知,此解决方案用于避免过度定价问题。