Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 使用实体框架6加载相关实体_Entity Framework - Fatal编程技术网

Entity framework 使用实体框架6加载相关实体

Entity framework 使用实体框架6加载相关实体,entity-framework,Entity Framework,这是我使用EntityFramework的第二天,我遇到了另一个问题: 我正在将某种配置保存到数据库中。这很有效: 这是我的班级: public class Configuration : IConfiguration {.... public ICollection<EmailClassificationPattern> emailClassificationPatterns { get; set; } ... } 然后我尝试访问成员emailClassificati

这是我使用EntityFramework的第二天,我遇到了另一个问题:

我正在将某种配置保存到数据库中。这很有效: 这是我的班级:

public class Configuration : IConfiguration
{....
   public ICollection<EmailClassificationPattern> emailClassificationPatterns { get; set; }
   ...
}
然后我尝试访问成员emailClassificationPatterns这是:

    var relevantSubjectList = configuration.emailClassificationPatterns.
          Select( x => x.subjectPatternForMailCollection);
但我得到一个ReferenceNullException,因为emailClassificationPatterns为NULL。EntityFramework为什么不加载引用的成员?我无法包含引用的成员,因为IntelliSense不提供.Inlcude()(如下所示:)

我是否有来启用加载?如果是-如何?是否有其他方法访问引用的成员,是否有某种最佳实践

致意


Sebastian

Where
前面是
Include
语句。这将加载“子”实体。像这样:

var configuration = Configuration)mycontext.Configurations.Include("emailClassificationPattern").Where(x => 
       x.configurationName=="Default")

是否还有另一种可能,我不必显式地指定还应该加载哪个子Memeber类?我希望EF能够识别需要哪些子成员(基于代码)并动态加载这些元素……不。您必须显式地加载它们。his也很聪明,因为在大型关系数据库中可能有大量相关的子实体(child-child-etc.)。但是……你也可以只加载配置实体,当你枚举配置的某些子实体时,如果我没记错的话,这些子实体将按需加载(及时加载)。谷歌是你的朋友!关于在EF中加载的大量文档;)
var configuration = (Configuration)mycontext.Configurations.Where(x => 
       x.configurationName=="Default").Include("emailClassificationPattern")
var configuration = Configuration)mycontext.Configurations.Include("emailClassificationPattern").Where(x => 
       x.configurationName=="Default")