具有跟踪对象的EF Core更新

问题描述 投票:1回答:1

我使用ABP框架,我确实在更新问题上苦苦挣扎。我使用实体frmaework核心。

我将尽我所能尽力而为。

我有一个类别表,其中包含类别名称和类别详细信息。

然后,我正在构建一个有角度的UI,该UI允许选择类别并将其存储在成就对象中。

然后,我的目标是存储成就和类别。

然后我创建了一个表AccomplishementCategoryCustoms。

所以我的模型如下:

[Table("Accomplishments")]
public class Accomplishment : AuditedEntity 
{

    [Required]
    public virtual string Name { get; set; }

   public List<AccomplishementCategoryCustom> Categories { get; set; }
}

[Table("AccomplishementCategoryCustoms")]
public class AccomplishementCategoryCustom : CreationAuditedEntity 
{


    public virtual int? CategoryId { get; set; }

    [ForeignKey("CategoryId")]
    public Category CategoryFk { get; set; }

    public virtual int? AccomplishmentId { get; set; }

    [ForeignKey("AccomplishmentId")]
    public Accomplishment AccomplishmentFk { get; set; }

}

我与类别和AccomplishementCategoryCustoms之间没有关系。

然后插入时效果很好。

当我更新并添加包含项时,出现如下错误:

With include

当我更新而没有包含时,出现错误:

Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。有关详细信息,请参见内部异常。 ---> System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'AccomplishementCategoryCustoms'中的标识列插入显式值。

最后,模型构建器如下:

 migrationBuilder.CreateTable(
            name: "AccomplishementCategoryCustoms",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                CreationTime = table.Column<DateTime>(nullable: false),
                CreatorUserId = table.Column<long>(nullable: true),
                CategoryId = table.Column<int>(nullable: true),
                AccomplishmentId = table.Column<int>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AccomplishementCategoryCustoms", x => x.Id);
                table.ForeignKey(
                    name: "FK_AccomplishementCategoryCustoms_Accomplishments_AccomplishmentId",
                    column: x => x.AccomplishmentId,
                    principalTable: "Accomplishments",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_AccomplishementCategoryCustoms_Categories_CategoryId",
                    column: x => x.CategoryId,
                    principalTable: "Categories",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_AccomplishementCategoryCustoms_AccomplishmentId",
            table: "AccomplishementCategoryCustoms",
            column: "AccomplishmentId");

        migrationBuilder.CreateIndex(
            name: "IX_AccomplishementCategoryCustoms_CategoryId",
            table: "AccomplishementCategoryCustoms",
            column: "CategoryId");
    }

我希望它将提供足够详细的修复信息,因为很长一段时间以来我一直在努力解决此问题,因此无法解决。

c# entity-framework-core aspnetboilerplate
1个回答
0
投票

好,所以我尝试使用以下代码:

  var accomplishment = _accomplishmentRepository.GetAll().AsNoTracking().Where(a => 
  a.Id ==input.Id.Value)
  .Include(a => a.Categories)
  .FirstOrDefault();

  var updatedEntity =  ObjectMapper.Map(input, accomplishment);

   await _accomplishmentRepository.UpdateAsync(updatedEntity);

我不再有错误,但是它在数据库中为相同的ID创建了两行。如何解决?

enter image description here

我真的不知道如何解决。

© www.soinside.com 2019 - 2024. All rights reserved.