gpt4 book ai didi

.net - 在EF Core映射上添加唯一索引似乎不起作用

转载 作者:行者123 更新时间:2023-12-04 23:36:36 26 4
gpt4 key购买 nike

我们在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();

任何帮助是极大的赞赏。

最佳答案

InMemory不是关系数据库。

InMemory will allow you to save data that would violate referential integrity constraints in a relational database.



如果要测试行为更像真正的关系数据库的东西,请考虑使用 SQLite in-memory模式。

引用: InMemory is not a relational database

关于.net - 在EF Core映射上添加唯一索引似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com