我在 Visual Studio 和 C# 中使用 Effort 来重新创建数据库并对其进行单元测试。我正在为单个表编写一个类,因此我只使用该表中的对象填充数据库。我的问题是 Effort 数据库希望对象的外键引用数据库中的实际对象。
我的错误是
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
NMemory.Exceptions.ForeignKeyViolationException: Foreign key violation [Table1 :: SettingsId]. The key value [0] does not exists in the referenced table [Table2:: SettingsId].. Error code: RelationError
由于这个特定的表有许多外键指向具有许多其他外键的对象,因此需要大量工作。 Effort 中是否有任何方法可以关闭此功能,以便我可以单独测试此表?
最佳答案
我也遇到了这个问题,想在更大的数据库范围内测试表。您可以制作一个辅助方法,该方法将只实例化您需要的所有内容,并作为结果传递带有引用的数据库或表。这很有帮助,因为您可以在需要的任何测试方法/类中随时调用它。
关于c# - 在 Effort 中取消强制外键引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489414/