c# - DbEntityValidationException - 必填字段为空

标签 c# entity-framework

当我使用 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);

最佳答案

如果您在模型类上使用 Require 属性,则必须允许空字符串。

[Required(AllowEmptyStrings =true)]

关于c# - DbEntityValidationException - 必填字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893619/

相关文章:

c# - 帮助设计订单管理器类

c# - 是否有用于 C# 的 Linq to REST 库

c# - 将导航属性映射到实例变量作为外键

c# - 如何使用 Entity Framework ASP.Net MVC 5 删除多条记录?

c# - 从另一个线程更新列表框后,如何强制主GUI线程更新列表框?

c# - TcpListener 常量错误 每个地址只使用一次

C# WPF 与 MySql 和 EntityFramework

c# - 在不重新查询数据库的情况下,在另一个 LINQ 查询中重用 LINQ 查询结果

c# - "The model backing the ' DataContext ' context has changed"但我正在进行相同的迁移

c# - Entity Framework/code first/table-per-type inheritance——实现派生类和具体类之间的一对多关联