1011_对一个或多个实体的验证失败。

原因:

可能1:MachineFaultRepair.cs 中存在另一个实体 public User Operator { get; set; },若Operator不存在,就出现上述错误。
可能2:[Required]、Include()

        [Required]
        [StringLength(50)]
        public string BedNo { get; set; }

                AppDatabase.DoAction(dbContext =>
                {
                    machine = dbContext.Machine
                                         //1011修改
                                        .Include(d => d.Product)
                                        .FirstOrDefault(x => x.Id == viewMachine.Id);
                });

修改方案1:

删除[Required]、Include()

修改方案2:

dbContext.Entry(model).State = EntityState.Modified;
dbContext.Configuration.ValidateOnSaveEnabled = false;

修改方案3:

namespace EFModel
{
    using ……;

    [Table("MachineFaultRepair")]
    public partial class MachineFaultRepair
    {
        /// 
        /// 操作者
        /// 备注:如果没有登录,默认为管理员,管理员的Id = 1
        /// 
        public int OperatorId
        {
            get
            {
                return OperatorId == 0 ? 1 : OperatorId;
            }
            set
            {
                OperatorId = value;
            }
        }

        //=======================NotMapped=========================

        [ForeignKey("OperatorId")]
        public User Operator { get; set; }

        [NotMapped]
        public string OperatorName { get { return Operator != null ? Operator.UserName : ""; } }
    }
}

错误:

对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

   在 System.Data.Entity.Internal.InternalContext.SaveChanges()
   在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   在 System.Data.Entity.DbContext.SaveChanges()
   在 HemoSystemClient.Forms.DialysisMaintenanceForm.<>c__DisplayClass26_0.b__0(AppDbContext dbContext) 位置 D:\rjyxProject\HemoSystem\HemoSystemClient\Forms\DialysisMaintenanceForm.cs:行号 461
   在 HemoSystemClient.AppDatabase.DoAction(Action`1 action, Boolean showException) 位置 D:\rjyxProject\HemoSystem\HemoSystemClient\AppDatabase.cs:行号 21

你可能感兴趣的:(1011_对一个或多个实体的验证失败。)