I use ABP Framework and I am really struggling on an update issue. I use entity frmaework core.
I am gonna try to ba as clear as I can.
I have a category table which hosts category names and details of the category.
Then I am building an angular UI which allows to select categories and store them in an accomplishement object.
Then my goal is to store Accomplishement and categories.
I then created a table AccomplishementCategoryCustoms.
So my model is as below:
[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; }
}
I do not have relations between categories and AccomplishementCategoryCustoms.
Then when I insert it works well.
When I update and add the include I have the error as below:
When I update without include I have an error:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'AccomplishementCategoryCustoms' when IDENTITY_INSERT is set to OFF.
FInally the modelbuilder is as below:
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");
}
I hope it will be enough detailed to have the fix because I am struggled on it since a long time and can't sort it out.
UpdateAsync
. I am assuming that what is being done is that the entity is being attached to the database context for the repository and thenSaveChangesAsync
is being called on it. The issue with doing this is that if you are attaching an object that was just returned from the query you performed prior, Entity Framework will have already started tracking that instance, so it will fail upon attempting to attach it again. The solution is to not attach it explicitly, and simply callSaveChangesAsync
on the context.