Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# 刷新EF6数据库上下文_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 刷新EF6数据库上下文

C# 刷新EF6数据库上下文,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我正在使用Entity Framework 6,我有一个product对象,其中包含一系列变体,如下所示: public class Product { public int ProductId { get; set;} public virtual ICollection<Variant> Variants { get; set;} ... other properties } public class Variant { public int

我正在使用Entity Framework 6,我有一个product对象,其中包含一系列变体,如下所示:

public class Product
{
    public int ProductId { get; set;}

    public virtual ICollection<Variant> Variants { get; set;}

    ... other properties
}

public class Variant
{
    public int VariantId { get; set;}

    public int ProductId { get; set;}

    public virtual Product Product { get; set;}

    public int StoreId { get; set;}

    ... other properties
}
现在这一切都很好,当我得到的产品,它回来与变种。然而,今天早上我愚蠢地从上下文中使用了产品,而不是DTO,并根据StoreId过滤了变体。现在,每当我获得产品时,它只返回该存储的变体,即使我从未提交任何更改

我已经检查了数据库,所有的变体仍然存在,所以我如何重置我的上下文,以便再次获得所有变体

我尝试了以下方法:

重置iis 清洗和重建溶液 更改对象以返回额外属性 使用以下方法重新加载产品:

foreach (Product product in context.Products) { context.Entry(product).Reload(); }
但是似乎什么都不起作用,我还需要做些什么来重置上下文吗?

因为结果是变量实体的配置错误,而不是过滤的上下文。我只使用VariantID作为键,但由于存在多个具有相同id的变量,因此在查询后映射回对象时,它使用与第一个具有该id的变量相同的值覆盖以下变量,使其看起来好像只有我筛选过的存储

我通过使密钥对存储区和变体id唯一来修复此问题:

HasKey(v => new {v.VariantId, c.StoreId});

你会永远保持上下文吗?你应该创建一个新的上下文,以便“刷新”所有内容。我不认为我会永远保留上下文-请参见上面的编辑,它位于using语句中,所以我认为它刚刚被调用,然后被处理。如果你像你一样创建一个新上下文,它就是新的。没有缓存早期的任何内容。它确实连接到了你认为它是的数据库吗?@CharlesMager,它肯定连接到了正确的数据库,如果我只是调用一个变量,它就会得到正确的变量,但是当我作为产品模型的一部分调用时,它只会得到我之前筛选的那些变量
HasKey(v => new {v.VariantId, c.StoreId});