我们在ASP.NET核心API解决方案中使用EF Core。数据库中有一个事务表,并且有一个包含两列的唯一索引。因此,此表中应该没有与这些列具有相同值的记录。
在我们的EF映射中,我们有这个
builder.HasIndex(e => new { e.RsaSessionId, e.SessionId }).IsUnique(true).HasName("IX_Transactions");
builder.Property(e => e.RsaSessionId)
.HasColumnName(nameof(Transaction.RsaSessionId))
.IsRequired();
builder.Property(e => e.SessionId)
.HasColumnName(nameof(Transaction.SessionId))
.IsRequired()
.HasColumnType("uniqueidentifier");
但是在我们使用内存数据库提供程序的集成测试中,当在DbContext的Transactions DbSet中添加两个相同的事务对象时,不会引发错误。这段代码不应该引发错误,因为我们已经指定有一个包含这两列的唯一键吗?
var rsaSessionID=1;
Guid issuerSessionId=Guid.NewGuid();
//create two transactions with the same values for the two fields in the unique index
var transaction = new Transaction(rsaSessionID, issuerSessionId, ... other fields ... );
var transaction = new Transaction(rsaSessionID, issuerSessionId, ... other fields ... );
this.fixture.Context.Transactions.Add(transaction);
this.fixture.Context.Transactions.Add(transaction2);
this.fixture.Context.SaveChanges();
任何帮助是极大的赞赏。