I have two related tables:
public class InspectionItems { [Key] public int InspectionItemID { get; set; } ......... [StringLength(10)] public string QualiteGradeID { get; set; } [ForeignKey("QualiteGradeID")] public virtual QualityGrades QualityGrades { get; set; } } public partial class QualityGrades { [Key] [StringLength(10)] public string QualiteGradeID { get; set; } [Required] [StringLength(100)] public string Title { get; set; } } InspectionItems inspectionItem = _unitOfWork.InspectionItems.All().FirstOrDefault(it => it.InspectionItemID == item.Id); if (inspectionItem != null) { inspectionItem.QualiteGradeID = "RR"; _unitOfWork.InspectionItems.Update(inspectionItem); _unitOfWork.Commit(); } public override void Update(T entity) { if (entity == null) { throw new ArgumentException("Cannot update a null entity."); } _dbSet.Local.Clear(); _dbSet.Attach(entity); _dbContext.Entry(entity).State = EntityState.Modified; } I need to update the QualiteGradeID field of the InspectionItems table, the problem is that this field may be NULL in the database, and when I try to update it, I generate SQL:
UPDATE [dbo].[InspectionItems] SET [QualiteGradeID] = NULL WHERE ([InspectionItemID] = @1 ) I make an experiment: I prohibit NULL values in the QualiteGradeID field (the InspectionItems table) - and everything starts working. Maybe someone faced with such behavior, I will be very grateful for the help.