我有一個問題,添加擁有的物體會導致 EF Core 發出UPDATE陳述句而不是INSERT,從而導致并發例外。
我有以下課程(簡化以使問題更短):
public abstract class PaymentDemand
{
public Guid Id { get; set; }
public SettlementTransactions? SettlementTransactions { get; set; }
// And more properties
}
public class SettlementTransactions
{
public List<SettlementAllowance> ConsumedAllowances { get; set; } = new List<SettlementAllowance>();
public List<SettlementCharge> GeneratedCharges { get; set; } = new List<SettlementCharge>();
}
public class SettlementAllowance
{
public Guid PaymentDemandId { get; set; }
/// <summary>The type of the transaction.</summary>
public string TransactionType { get; set; }
// And more properties
}
然后我設定了付款需求:
public void Configure(EntityTypeBuilder<PaymentDemand> builder)
{
builder.HasKey(p => p.Id);
builder.Property(p => p.Id).ValueGeneratedNever();
builder.OwnsOne(p => p.SettlementTransactions, transactionBuilder =>
{
transactionBuilder.OwnsMany(a => a.ConsumedAllowances, allowanceBuilder =>
{
allowanceBuilder.WithOwner().HasForeignKey(t => t.PaymentDemandId);
allowanceBuilder.HasKey(p => p.AccountTransactionId);
allowanceBuilder.Property(p => p.Amount).HasColumnType("Money");
allowanceBuilder.ToTable("SettlementAllowances");
});
transactionBuilder.OwnsMany(c => c.GeneratedCharges, chargesBuilder =>
{
chargesBuilder.WithOwner().HasForeignKey(t => t.PaymentDemandId);
chargesBuilder.HasKey(p => p.AccountTransactionId);
chargesBuilder.Property(p => p.AccountTransactionId).ValueGeneratedNever();
chargesBuilder.Property(p => p.Amount).HasColumnType("Money");
chargesBuilder.ToTable("SettlementCharges");
});
});
builder.Navigation(t => t.SettlementTransactions).IsRequired();
}
最后,我有進行保存的代碼。
public async Task DoThings(DbContext context)
{
var demand = await context.PaymentDemands
.Include(st => st.SettlementTransactions)
.FirstOrDefaultAsync(d => d.Id == id, default);
if (demand == null)
{
throw new InvalidOperationException();
}
demand.SettleDate = DateTime.UtcNow;
demand.SettlementTransactions ??= new SettlementTransactions();
demand.SettlementTransactions.ConsumedAllowances.Add(new SettlementAllowance{ TransactionType = "Blah" });
await context.SaveChangesAsync(); // THROWS.
}
更改跟蹤器似乎正在確定它應該做什么(來自日志):
DetectChanges starting for 'SqlBillingContext'.
2 entities were added and 0 entities were removed from navigation 'SettlementTransactions.ConsumedAllowances' on entity with key '{PaymentDemandId: d00544fe-c6c5-4b1d-a438-81508332af2d}'.
Context 'SqlBillingContext' started tracking 'SettlementAllowance' entity with key '{AccountTransactionId: 296596b3-d45d-4753-b2f5-0ba7566f6800}'.
Context 'SqlBillingContext' started tracking 'SettlementAllowance' entity with key '{AccountTransactionId: 7395965f-7fda-46d9-9e3b-3b428d4a7dce}'.
但由于某種原因,它最終會在更新之后結束:
Executing update commands individually as the number of batchable commands (2) is smaller than the minimum batch size (4).
Executing DbCommand [Parameters=[@p11='296596b3-d45d-4753-b2f5-0ba7566f6800', @p0='2022-10-25T09:34:41.7143804 00:00', @p1='49.5' (Precision = 3) (Scale = 1), @p2=NULL (Size = 4000), @p3='2022-12-24T09:18:36.3176378 00:00' (Nullable = true), @p4='6ca8c244-4438-4476-b417-ed501c378c0e' (Nullable = true), @p5='12032' (Nullable = true), @p6='d00544fe-c6c5-4b1d-a438-81508332af2d', @p7='df99b62b-d1b6-4d58-8c69-4df7eef33b61' (Nullable = true), @p8='d00544fe-c6c5-4b1d-a438-81508332af2d' (Nullable = true), @p9='2022-11-24T09:18:37.3176378 00:00' (Nullable = true), @p10='1'], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
UPDATE [SettlementAllowances] SET [AccountingTime] = @p0, [Amount] = @p1, [Description] = @p2, [EndTime] = @p3, [InvoiceId] = @p4, [InvoiceNumber] = @p5, [PaymentDemandId] = @p6, [PaymentId] = @p7, [SourcePaymentDemandId] = @p8, [StartTime] = @p9, [TransactionType] = @p10
WHERE [AccountTransactionId] = @p11;
SELECT @@ROWCOUNT;
我真的不明白這里發生了什么。有什么建議、想法或明顯的原因導致這不能像我希望的那樣作業嗎?
uj5u.com熱心網友回復:
通常情況下,寫這些帖子幾乎就像橡皮擦一樣。
我的問題是我有我創建的物體的主鍵(因為我基本上是在移動它)。因此 EF 認為它需要生成一個不存在的密鑰,并且提供了一個,它假定記錄存在(默認行為)。
解決方案是簡單地添加 NEVER 的生成策略:
allowancesBuilder.Property(p => p.AccountTransactionId).ValueGeneratedNever();
我已經對費用和付款了!
默認顯然是ValueGeneratedSometimes....
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522869.html
標籤:实体框架实体框架核心
