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

时间:2017-11-27 10:28:41

标签: entity-framework entity-framework-6 aspnetboilerplate identity-insert

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

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

  

无法在表中插入identity列的显式值   IDENTITY_INSERT设置为OFF时的'SemesterDetails'

这是Master表:

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; }
}

这是Details表,由StudentSemesterCore类表示:

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相关的所有其他数据。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我试过这个

 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;
            }