Gavin Coates
Gavin Coates

Reputation: 1425

Entity Framework Add() Is Updating Instead Of Inserting

I have the following function for adding an object to the database:

public void AddEnterpriseShare(Objects.FinancialTransactionEnterpriseShare share)
{
    _services.DbContext.FinancialTransactionEnterpriseShares.Add(share);
    _services.DbContext.SaveChanges();
}

I call it as follows:

foreach (var enterprise in model.Enterprise)
{
    Service.FinancialTransactionServices.AddEnterpriseShare(new Data.Objects.FinancialTransactionEnterpriseShare()
    {
       ProfitLossId = profitLoss.Id,
       EnterpriseId = enterprise.Id,
       Percentage = enterprise.Percent,
       FinancialTransactionId = transaction.Id
   });
}

This loop should run 6 times, and add 6 new records into the database, one for each Id. If I step through this code though, I can see the record being created int he database.. then on second time through the loop, the original record is being updated, rather than a second record being inserted!

Why am I seeing this behaviour?

The entity class is as follows:

public class FinancialTransactionEnterpriseShare
{
    public int Id { get; set; }
    public int ProfitLossId { get; set; }
    public int EnterpriseId { get; set; }
    public decimal Percentage { get; set; }
    public int FinancialTransactionId { get; set; }

    public ProfitLoss ProfitLoss { get; set; }
    public Enterprise Enterprise { get; set; }
    public FinancialTransaction FinancialTransaction { get; set; }
}

migrationBuilder.CreateTable(
            name: "FinancialTransactionEnterpriseShares",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                EnterpriseId = table.Column<int>(nullable: false),
                FinancialTransactionId = table.Column<int>(nullable: false),
                Percentage = table.Column<decimal>(nullable: false),
                ProfitLossId = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_FinancialTransactionEnterpriseShares", x => x.Id);
                table.ForeignKey(
                    name: "FK_FinancialTransactionEnterpriseShares_Enterprises_EnterpriseId",
                    column: x => x.EnterpriseId,
                    principalTable: "Enterprises",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.NoAction);
                table.ForeignKey(
                    name: "FK_FinancialTransactionEnterpriseShares_FinancialTransactions_FinancialTransactionId",
                    column: x => x.FinancialTransactionId,
                    principalTable: "FinancialTransactions",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.NoAction);
                table.ForeignKey(
                    name: "FK_FinancialTransactionEnterpriseShares_ProfitLosses_ProfitLossId",
                    column: x => x.ProfitLossId,
                    principalTable: "ProfitLosses",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.NoAction);
            });

        migrationBuilder.CreateIndex(
            name: "IX_FinancialTransactionEnterpriseShares_EnterpriseId",
            table: "FinancialTransactionEnterpriseShares",
            column: "EnterpriseId");

        migrationBuilder.CreateIndex(
            name: "IX_FinancialTransactionEnterpriseShares_FinancialTransactionId",
            table: "FinancialTransactionEnterpriseShares",
            column: "FinancialTransactionId",
            unique: true);

        migrationBuilder.CreateIndex(
            name: "IX_FinancialTransactionEnterpriseShares_ProfitLossId",
            table: "FinancialTransactionEnterpriseShares",
            column: "ProfitLossId");

Upvotes: 2

Views: 601

Answers (1)

Gavin Coates
Gavin Coates

Reputation: 1425

Thanks to Ivan Stoevs comments above, I have now found the issue.

The FinancialTransaction class contains a reference to the FinancialTransactionEnterpriseSplit class - this reference was set as 1:1 rather than being 1:*. As a result of this, when generated, the FinancialTransactionId field was being created with the unique flag set to true.

Updating the code to use ICollection<FinancialTransactionEnterpriseSplit> in FinancialTransaction solved the issue, causing the field to be created correctly.

Upvotes: 3

Related Questions