Reputation: 2248
I get this error when I save changes to my Db using EF. I'm using the following code to dig into the exception:
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 gives me these 2 exception messages: 1.) flv_url field is required. 2.) org_url field is required. I suspected that I might be passing null so I tried using empty strings but I still get this error.
Here's the code which saves the model into the 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();
}
Am I getting the error because I'm passing empty strings? I've never encountered this error before.
Edit
Model class looks like this:
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; }
}
here's the fluent API mappings for the Model:
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);
Upvotes: 2
Views: 894
Reputation: 571
If you are using Require
attribute on your model class, you must allow empty strings.
[Required(AllowEmptyStrings =true)]
Upvotes: 6