当我使用EF保存对Db的更改时出现此错误。我使用以下代码深入研究异常:
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
eve.ValidationErrors给了我这两条异常消息: 1.)flv_url字段是必需的。 2.)org_url字段是必需的。 我怀疑我可能会传递null,所以我尝试使用空字符串,但我仍然遇到此错误。
这是将模型保存到Db中的代码。
DataModel db = new DataModel();
string userName = vid.UserName;
var vid_list = db.videos.Where(v => v.username == userName).OrderByDescending(d => d.date_added).ToList();
var nvid = vid_list[0];
if (nvid != null)
{
nvid.title = vid.Title;
nvid.categories = vid.Categories;
nvid.videofilename = vid.VideoFileName;
nvid.originalvideofilename = vid.OriginalVideoFileName;
nvid.thumbfilename = vid.ThumbFileName;
nvid.flv_url = "";
nvid.org_url = "";
db.Entry(nvid).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
我收到错误是因为我传递空字符串了吗?我之前从未遇到过这个错误。
修改
模型类如下所示:
public partial class video
{
public long videoid { get; set; }
public short? categoryid { get; set; }
[Required]
[StringLength(20)]
public string username { get; set; }
[StringLength(100)]
public string title { get; set; }
[StringLength(100)]
public string search_term { get; set; }
[Column(TypeName = "text")]
public string description { get; set; }
[StringLength(200)]
public string tags { get; set; }
[StringLength(20)]
public string duration { get; set; }
public int views { get; set; }
public int favorites { get; set; }
public int total_rating { get; set; }
public int comments { get; set; }
public int responses { get; set; }
public float ratings { get; set; }
public float avg_rating { get; set; }
[Required]
[StringLength(50)]
public string videofilename { get; set; }
[Required]
[StringLength(50)]
public string thumbfilename { get; set; }
[StringLength(100)]
public string originalvideofilename { get; set; }
[Column(TypeName = "text")]
public string embed_script { get; set; }
public byte isenabled { get; set; }
public byte isprivate { get; set; }
public byte iscomments { get; set; }
public byte isratings { get; set; }
public byte isresponse { get; set; }
public byte isfeatured { get; set; }
public byte isexternal { get; set; }
public byte isadult { get; set; }
public int response_videoid { get; set; }
public int duration_sec { get; set; }
public byte ispublished { get; set; }
public byte isreviewed { get; set; }
[Required]
[StringLength(200)]
public string flv_url { get; set; }
[Required]
[StringLength(200)]
public string org_url { get; set; }
[Required]
[StringLength(200)]
public string thumb_url { get; set; }
public byte errorcode { get; set; }
public DateTime? date_added { get; set; }
[StringLength(15)]
public string ipaddress { get; set; }
public byte type { get; set; }
public int liked { get; set; }
public int disliked { get; set; }
[StringLength(150)]
public string youtubeid { get; set; }
public byte istagsreviewed { get; set; }
[StringLength(200)]
public string categories { get; set; }
[Required]
[StringLength(20)]
public string language { get; set; }
public int downloads { get; set; }
public byte mode { get; set; }
[StringLength(10)]
public string authkey { get; set; }
public long galleryid { get; set; }
[StringLength(200)]
public string coverurl { get; set; }
}
这里是模型的流畅API映射:
modelBuilder.Entity<video>()
.Property(e => e.username)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.title)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.search_term)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.description)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.tags)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.duration)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.videofilename)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.thumbfilename)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.originalvideofilename)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.embed_script)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.flv_url)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.org_url)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.thumb_url)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.ipaddress)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.youtubeid)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.categories)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.language)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.authkey)
.IsUnicode(false);
modelBuilder.Entity<video>()
.Property(e => e.coverurl)
.IsUnicode(false);
答案 0 :(得分:4)
如果在模型类上使用Require
属性,则必须允许空字符串。
[Required(AllowEmptyStrings =true)]