EF Core:将IDENTITY_INSERT一对一设置为OFF时,无法为表中的标识列插入显式值

时间:2018-08-07 10:05:44

标签: c# entity-framework-core ef-fluent-api

我将EF Core和ASP NET Core一起用于服务器,当我尝试更新数据库中的现有值时,出现以下错误:

  

当IDENTITY_INSERT设置为OFF时,无法为表“ TeambuildingType”中的标识列插入显式值。

我正在做什么:

  1. 创建一个Teambuilding元素,并将TeamBuildingTypeId的外键最初设置为NULL

  2. 使用INSERT INTO ...直接从SQL Management Studio创建两个TeambuildingType。(对于Teambuilding和TeambuildingType,ID会自动递增)

  3. 尝试通过添加如下的TeambuildingTypeId来更新现有的Teambuilding:team.TeambuildingTypeId = 1或team.Type =(从相同上下文中从数据库获取的对象)

  4. 从上面捕获错误

这是我的代码:

TeamBuilding.cs

public class TeamBuilding
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime EndDate { get; set; }

    public double? TargetBudget { get; set; }

    public TeambuildingStatus? Status { get; set; }

    public int? TeambuildingTypeId { get; set; }

    public virtual TeambuildingType Type { get; set; }

    public int? LocationId { get; set; }

    public virtual Location Location { get; set; }

    public virtual ICollection<Participant> Participants { get; set; }

    public virtual ICollection<Room> Rooms { get; set; }

    public int TimePollId { get; set; }

    public virtual TimePoll TimePoll { get; set; }
}

TeambuildingType.cs

public class TeambuildingType
{
    [Key]
    public int Id { get; set; }

    public string Type { get; set; }

    public virtual ICollection<TeamBuilding> Teambuildings { get; set; }
}

TeamBuildingForUpdateDto.cs

public class TeamBuildingForUpdateDto
{
    [Required]
    public int Id { get; set; }
    public string Title { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime StartDate { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime EndDate { get; set; }
    public LocationViewModel Location { get; set; }
    public TeambuildingStatus Status { get; set; }
    public double TargetBudget { get; set; }
    public TeamBuildingTypeDto Type { get; set; }
}

更新控制器方法:

    [HttpPut]
    public IActionResult UpdateTeamBuilding([FromBody]TeamBuildingForUpdateDto teamBuildingForUpdateDto)
    {
        try
        {
            var existingTeamBuilding = _service.GetByID(teamBuildingForUpdateDto.Id);
            if (existingTeamBuilding == null)
            {
                return NotFound("There is no team buiding with such an ID");
            }

            _service.UpdateTeamBuilding(teamBuildingForUpdateDto);
            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

服务方法:

    public TeamBuildingForUpdateDto UpdateTeamBuilding(TeamBuildingForUpdateDto teamBuildingDto)
    {
        var teamBuilding = _repositoryTeam.GetByID(teamBuildingDto.Id);
        var type = _repositoryType.GetByID(teamBuildingDto.Type.Id);
        Mapper.Map(teamBuildingDto.Type, type);
        Mapper.Map(teamBuildingDto, teamBuilding);

        teamBuilding.Type = type;
        //OR
        //teamBuilding.TeambuildingTypeId = type.Id;
        //Neither from above works

        _repositoryTeam.Edit(teamBuilding);
        _repositoryTeam.Commit();
        return teamBuildingDto;
    }

使用Fluent API的上下文:

            modelBuilder.Entity<Models.TeamBuilding>()
            .HasOne(t => t.Type)
            .WithMany(ty => ty.Teambuildings)
            .HasForeignKey(t => t.TeambuildingTypeId);

            modelBuilder.Entity<TeambuildingType>().ToTable("TeambuildingType");
            modelBuilder.Entity<Models.TeamBuilding>().ToTable("TeamBuilding");


        public DbSet<TeambuildingType> TeambuildingTypes { get; set; }
        public DbSet<Models.TeamBuilding> TeamBuildings { get; set; }

我也不会仅在那些型号上收到此错误,在使用FK的任何地方以及尝试在其中插入新值的地方都会收到相同的信息。

TeamBuilding和TeambuildingType之间的关系是一对多的

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。根据mcbowes的建议,我检查了AutoMapper以及从服务器发送的内容,发现我正在尝试在TeamBuilding Type字段中分配TeamBuildingType,然后尝试进行更新。

我解决了问题,方法是不将任何TeamBuildingType分配给Type字段(使其为空),而仅将TeamBuildingType主键分配给TeambuildingTypeId字段。现在,它进行更新。

谢谢麦博威斯的建议。