实体框架核心:使用内存中的数据存储时如何测试导航属性加载 [英] Entity framework core: How to test navigation propery loading when use in-memory datastore

查看:92
本文介绍了实体框架核心:使用内存中的数据存储时如何测试导航属性加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架核心中存在一个有趣的功能:

There is an interesting feature exists in entity framework core:


实体框架核心将自动修复导航属性
到先前已加载到上下文
实例中的任何其他实体。因此,即使您没有显式包含
导航属性的数据,但如果先前已加载某些或
个所有相关实体,则该属性仍可能被填充。

Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded.

在某些情况下,这很好。但是,目前,我正在尝试使用高级语法添加功能对多对多关系进行建模,并且不想检查我创建的映射是否工作正常。

This is nice in some cases. However at the current moment I'm trying to modelling many-to-many relation with advanced syntaxic additions and wan't to check, that the mapping I create work well.

但是我实际上无法做到这一点,因为如果让我说类似的话:

But I actually can't do that, since if let's say I have something like:

class Model1{
   ... // define Id and all other stuff
   public ICollection<Model2> Rel {get; set;}
}

Model1 m1 = new Model1(){Id=777};
m1.Rel.Add(new Model2());
ctx.Add(m1);
ctx.SaveChanges()

var loaded = ctx.Model1s.Single(m => m.Id == 777);

因此由于自动修正 loaded.Rel 字段将被填充,即使我不包含任何内容。因此,有了此功能,我实际上什么也无法检查。无法检查我是否使用了正确的映射,并且我对 Include 的添加工作正常。牢记thouse,我应该改变什么才能能够实际测试我的导航属性正常工作?

so due to auto-fixup loaded.Rel field already will be populated, even if I don't include anything. So with this feature I can't actually check nothing. Can't check that I use proper mapping, and my additions to Include works properly. Having thouse in mind, what should I change to be able to actaully test my navigation properties work properly?

我创建了一个应该通过但现在失败的测试用例。 可以在此处找到确切的代码

I create a testcase which should be passing, but now failing. Exact code could be found there

我是

推荐答案

如果要在内存中测试导航属性,请使用.Net Core 2.0 Preview 1和EF Core。数据存储区,您需要使用 AsNoTracking()扩展名以非跟踪模式加载项目。

If you want to test navigation properties with in-memory data store, you need to load your items in "non-tracked" mode, using AsNoTracking() extension.

因此,对于您的情况,如果
var loading = ctx.Model1s.Single(m => m.Id == 777); 返回具有关联关系的项目,则将其重写为:

var loading = ctx.Model1s.AsNoTracking()。Single(m => m.Id == 777); 这将返回您没有原始的原始物品。

So, for your case if var loaded = ctx.Model1s.Single(m => m.Id == 777); return you item with relations, than if you rewrite to:
var loaded = ctx.Model1s.AsNoTracking().Single(m => m.Id == 777); this will return you raw item without deps.

因此,如果您想再次检查 Include ,则可以编写类似于 ctx的内容.Model1s.AsNoTracking()。Include(m => m.Rel).Single(m => m.Id == 777); ,这将返回包含所包含关系的模型。

So then if you want to check Include again, you could write something like ctx.Model1s.AsNoTracking().Include(m => m.Rel).Single(m => m.Id == 777); and this will return you model with relations you include.

这篇关于实体框架核心:使用内存中的数据存储时如何测试导航属性加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆