更新实体时无法为标识列插入显式值

Cannot insert explicit value for identity column while updating entity

我正在使用 ASP.NET 样板模板。

我想更新 Details table,其中包含不止一项。如果项目存在,则必须更新,否则必须添加新项目,并且必须删除与 Details table 中的 Master 主键相关的所有其他条目。但它显示错误:

Cannot insert explicit value for identity column in table 'SemesterDetails' when IDENTITY_INSERT is set to OFF

这是Master table:

public class StudentDegreeCore : Entity<int>
{
    [StringLength(150)]
    [Required(ErrorMessage = "Enter Degree College ")]
    public string DegreeCollege { get; set; }

    [Required()]
    public string CollegeID { get; set; }

    [StringLength(7, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 7)]
    [Required(ErrorMessage = "Enter 10th Pass Year")]
    public string CommencementYear { get; set; }

    public List<StudentSemesterCore> SemesterDetails { get; set; }
}

这是Detailstable,表示为StudentSemesterCoreclass:

public class StudentSemesterCore
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [StringLength(150)]
    [Required(ErrorMessage = "Enter Year/Semester")]
    public string YearOrSemester { get; set; }

    [Required()]
    public virtual int StudentDegreeID { get; set; }

    [ForeignKey("StudentDegreeID")]
    public virtual StudentDegreeCore StudentDegreeCore { get; set; }

    [StringLength(4, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
    [Required(ErrorMessage = "Enter Semester Status")]
    public string Status { get; set; }

    [DisplayName("% of Marks")]
    [RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessage = "Numbers With Two decimal Place Allowed")]
    public decimal MarkPercentage { get; set; }
}

这是 Update 代码:

_studentdegreeRepository.Update(st);
CurrentUnitOfWork.SaveChanges();

调用SaveChanges时显示错误。实际上,如果存在相同的值,我想更新详细信息,否则添加新的,并且必须删除与相同的 StudentDegreeID 相关的所有其他数据。

  1. StudentSemesterCore 不是从实体派生的。
  2. 您不需要将 Id 属性 放入 StudentSemesterCore。删除它。
  3. 将 StudentDegreeCoreId 添加到 StudentSemesterCore 作为外键引用。

我试过了

 public override async Task<StudentDegreeDto> Create(StudentDegreeCreateDto input)
            {
                //CheckCreatePermission();
                StudentDegreeCore st = new StudentDegreeCore();
                try
                {

                    StudentDegreeCore core = new StudentDegreeCore()
                    {
                        Id = input.Id,
                        Address1 = input.Address1,
                        Address2 = input.Address2,
                        City = input.City,
                        CollegeID = input.CollegeID,
                        CommencementYear = input.CommencementYear,
                        CompletionYear = input.CompletionYear,
                        CurrentYear = input.CurrentYear,
                        DegreeCollege = input.DegreeCollege,
                        DegreeId = input.DegreeId,
                        OverallPercent = input.OverallPercent,
                        PinCode = input.PinCode,
                        PostBox = input.PostBox,
                        State = input.State,
                        StreamId = input.StreamId,
                        UserId = input.UserId
                    };
                    core.SemesterDetails = new List<StudentSemesterCore>();

                    foreach (var items in input.SemesterDetails)
                    {
                        core.SemesterDetails.Add(new StudentSemesterCore()
                        {
                            GPA = items.GPA,
                            MarkPercentage = items.MarkPercentage,
                            Status = items.Status,
                            UserId = items.UserId ,
                            Id = items.Id,
                            StudentDegreeID = items.StudentDegreeID ,
                            YearOrSemester = items.YearOrSemester,
                            LastModificationTime = DateTime.Now,
                            CreationTime = DateTime.Now
                        });                          
                    }
                    var student = core; //ObjectMapper.Map<StudentDegreeCore>(input);
                    long uid = (AbpSession.UserId == null) ? 0 : Convert.ToInt64(AbpSession.UserId);
                    st = _studentRepository.Get(student.Id);
                    if (st != null && st.Id > 0)
                    {
                        st.DegreeCollege = student.DegreeCollege;
                        st.CollegeID = student.CollegeID;
                        st.CommencementYear = student.CommencementYear;
                        st.CompletionYear = student.CompletionYear;
                        st.LastModificationId = Convert.ToInt32(AbpSession.UserId);
                        st.LastModificationTime = DateTime.Now;
                        st.StreamId = student.StreamId;
                        st.DegreeId = student.DegreeId;
                        st.CurrentYear = student.CurrentYear;
                        st.OverallPercent = student.OverallPercent;
                        st.PinCode = student.PinCode;
                        st.PostBox = student.PostBox;
                        st.State = student.State;
                        st.Address1 = student.Address1;
                        st.Address2 = student.Address2;
                        st.City = student.City;
                        st.SemesterDetails = new List<StudentSemesterCore>();
                        //st.SemesterDetails = student.SemesterDetails;
                        _studentRepository.Update(st);
                        foreach (var items in student.SemesterDetails)
                        {
                            _studentSemesterRepository.InsertOrUpdate(items);
                        }
                        //_studentRepository.Update(st);                   
                        CurrentUnitOfWork.SaveChanges();
                    }
                    else
                    {
                        student.UserId = Convert.ToInt32(AbpSession.UserId);
                        student.CreationId = Convert.ToInt32(AbpSession.UserId);
                        _studentRepository.Insert(student);
                        CurrentUnitOfWork.SaveChanges();
                    }
                }
                catch (Exception ex)
                {

                }
                StudentDegreeDto studentDegreeDto = new StudentDegreeDto()
                {
                    Id = input.Id,
                    Address1 = input.Address1,
                    Address2 = input.Address2,
                    City = input.City,
                    CollegeID = input.CollegeID,
                    CommencementYear = input.CommencementYear,
                    CompletionYear = input.CompletionYear,
                    CurrentYear = input.CurrentYear,
                    DegreeCollege = input.DegreeCollege,
                    DegreeId = input.DegreeId,
                    OverallPercent = input.OverallPercent,
                    PinCode = input.PinCode,
                    PostBox = input.PostBox,
                    State = input.State,
                    StreamId = input.StreamId,
                    UserId = input.UserId
                };
                studentDegreeDto.SemesterDetails = new List<StudentSemesterDto>();
                foreach (var items in input.SemesterDetails)
                {
                    studentDegreeDto.SemesterDetails.Add(new StudentSemesterDto()
                    {
                        GPA = items.GPA,
                        MarkPercentage = items.MarkPercentage,
                        Status = items.Status,
                        YearOrSemester = items.YearOrSemester,
                        LastModificationTime = DateTime.Now,
                        CreationTime = DateTime.Now
                    });
                }
                return studentDegreeDto;
            }