原因:
可能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
{
/// <summary>
/// 操作者
/// 備注:如果沒有登錄,默認為管理員,管理員的Id = 1
/// </summary>
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.<DialysisMachineMaintenance>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