c# - 在 Effort 中取消强制外键引用

标签 c# effort

我在 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/

相关文章:

sql-server - 在没有实际的 SQL Server 数据库启动和运行的情况下,我将如何配置工作量测试工具来模拟 Entity Framework 的 DbContext?

c# - 未经授权的错误机器人框架

c# - 无法关闭showDialog

c# - C#调用mysql存储过程的方法

entity-framework - 使用计算值对 Entity Framework 6 进行单元测试

c# - ListView中的"Value does not fall within the expected range"异常

c# - 对 Windows Azure VPS 上的高 I/O 应用程序进行负载平衡

entity-framework-6 - 在数据库优先方法中将EF6与Effort结合使用

c# - EntityFramework、DbContextScope 和 Effort - 异常 : DbContext has been disposed inside unit test