我具有以下用于将对象添加到数据库的功能:
public void AddEnterpriseShare(Objects.FinancialTransactionEnterpriseShare share)
{
_services.DbContext.FinancialTransactionEnterpriseShares.Add(share);
_services.DbContext.SaveChanges();
}
我这样称呼它:
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
});
}
此循环应运行6次,并将6条新记录添加到数据库中,每个ID一条。如果我单步执行此代码,则可以看到在数据库中创建了记录。然后在第二次循环中,原始记录正在更新,而不是第二条记录被插入!
为什么我会看到这种行为?
实体类如下:
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");
答案 0 :(得分:1)
由于上面的Ivan Stoevs评论,我现在已经找到了问题。
FinancialTransaction
类包含对FinancialTransactionEnterpriseSplit
类的引用-此引用设置为1:1而不是1:*。结果,在生成FinancialTransactionId
字段时,其唯一标志设置为true。
更新代码以使用ICollection<FinancialTransactionEnterpriseSplit>
中的FinancialTransaction
解决了该问题,导致正确创建了字段。