Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DbEntityValidationException-必填字段为空_C#_Entity Framework - Fatal编程技术网

C# DbEntityValidationException-必填字段为空

C# DbEntityValidationException-必填字段为空,c#,entity-framework,C#,Entity Framework,当我使用EF保存对数据库的更改时,会出现此错误。我正在使用以下代码深入研究异常: catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the

当我使用EF保存对数据库的更改时,会出现此错误。我正在使用以下代码深入研究异常:

      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.)需要“组织url”字段。 我怀疑我可能会传递null,所以我尝试使用空字符串,但仍然会出现此错误

下面是将模型保存到数据库中的代码

            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; }
}
以下是模型的fluent 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);
modelBuilder.Entity()
.Property(e=>e.username)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.title)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.search\u术语)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.description)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.tags)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.duration)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.videofilename)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.thumbfilename)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.originalvideofilename)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.embed\u脚本)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.flv_url)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.org\u url)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.thumb\u url)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.ipaddress)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.youtubeid)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.categories)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.language)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.authkey)
.IsUnicode(假);
modelBuilder.Entity()
.Property(e=>e.coverurl)
.IsUnicode(假);

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


[必需(AllowEmptyStrings=true)]

你能发布你的模型类和任何流畅的API映射吗?@user2697817我已经更新了我的问题哦,好的,可能就是这样。谢谢