Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟链接的虚拟数据_C#_Linq_Mocking - Fatal编程技术网

C# 模拟链接的虚拟数据

C# 模拟链接的虚拟数据,c#,linq,mocking,C#,Linq,Mocking,我使用的是使用外键的一对多链接的数据库 例如,我尝试模拟数据(大大简化) 当我从数据库在上下文中构建并使用modelbuilder来告诉链接时,这是可行的,但我如何才能让它在模拟框架的外部工作?您试图进行的单元测试是什么?您需要提供更多信息,例如,您的数据访问层中有哪些实体(调用实体框架)的类?这就是我们在代码中分离数据和业务的原因。如果您使用了某种类型的数据抽象层,比如存储库模式,您就不会有这个问题。旁注:执行FirstOrDefault().Transaction是没有意义的,因为如果找不到

我使用的是使用外键的一对多链接的数据库

例如,我尝试模拟数据(大大简化)


当我从数据库在上下文中构建并使用modelbuilder来告诉链接时,这是可行的,但我如何才能让它在模拟框架的外部工作?

您试图进行的单元测试是什么?您需要提供更多信息,例如,您的数据访问层中有哪些实体(调用
实体框架
)的类?这就是我们在代码中分离数据和业务的原因。如果您使用了某种类型的数据抽象层,比如存储库模式,您就不会有这个问题。旁注:执行
FirstOrDefault().Transaction
是没有意义的,因为如果找不到它,将抛出空引用异常。最好先执行
First()。这个问题已经被问了一百万次了!
List<Account> account = new List<Account> {
    new Account { id = 1, name = "ABC" }
};

List<Transaction> trans = new List<Transaction {     
    new Transaction { AccountID = 1, Quant = 1 },
    new Transaction { AccountID = 1, Quant = 3 }
};
var a = unitofwork.Accounts.FirstorDefault().Transaction;