实体框架加载忽略include语句的所有相关实体 [英] Entity framework loading all related entities ignoring include statement

查看:55
本文介绍了实体框架加载忽略include语句的所有相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有我的EF配置版本:

 我的型号:

公共类项目
{
public int ID {get;组; }
公共字符串代码{get;组; }
public string Name {get;组; }
public string说明{get;组; }
public ProjectState State {get;组; }
public DateTime CreationDate {get;组; }
公共字符串CreatedByID {get;组; }
public virtual ApplicationUser CreatedBy {get;组; }
public DateTime ModificationDate {get;组; }
公共字符串ModifiedByID {get;组; }
public virtual ApplicationUser ModifiedBy {get;组; }
public int RevisionIndex {get;组; }
}

这是我的流畅API配置:

 this.ToTable(" tblProject"); 
this.Property(t => t.ID).HasColumnName(" ID");
this.Property(t => t.Code).HasColumnName(" Code");
this.Property(t => t.Name).HasColumnName(" Name");
this.Property(t => t.Description).HasColumnName(" Description");
this.Property(t => t.CreatedByID).HasColumnName(" CreatedByID");
this.Property(t => t.CreationDate).HasColumnName(" CreationDate");
this.Property(t => t.ModifiedByID).HasColumnName(" ModifiedByID");
this.Property(t => t.ModificationDate).HasColumnName(" ModificationDate");
this.Property(t => t.State).HasColumnName(" State");
this.Property(t => t.RevisionIndex).HasColumnName(" RevisionIndex");

HasRequired(t => t.CreatedBy).WithMany(a => a.CreatedProjects).HasForeignKey(t => t.CreatedByID);
HasRequired(t => t.ModifiedBy).WithMany(a => a.ModifiedProjects).HasForeignKey(t => t.ModifiedByID);

我的DAL存储库如下所示:

 public ProjectRepository(ActivityTrackerDbContext context)
{
this.context = context;
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = false;
}

public IEnumerable< Project> GetProjects()
{
var x = context.Projects.Include(" CreatedBy")。ToList();
返回x;
}

问题在于,尽管我只包含了CreatedBy属性,但MOdifiedBy也已填充。我做错了什么或错过了什么?


lazyLoading = false,导航属性是虚拟的,但加载一个属性我正在加载另一个...任何帮助appriciated ...




解决方案

您好,


您可以考虑使用
DTO
(数据传输对象)类与
AutoMapper
相切。


Hello, I have issie with my EF configuration:
 my model:

public class Project
    {
        public int ID { get; set; }
        public string  Code { get; set; }
        public string  Name { get; set; }
        public string Description { get; set; }
        public ProjectState State { get; set; }
        public DateTime CreationDate { get; set; }
        public string CreatedByID { get; set; }
        public virtual ApplicationUser CreatedBy { get; set; }
        public DateTime ModificationDate { get; set; }
        public string ModifiedByID { get; set; }
        public virtual ApplicationUser ModifiedBy  { get; set; }
        public int RevisionIndex { get; set; }
    }

and here is my fluent API config:

  this.ToTable("tblProject");
            this.Property(t => t.ID).HasColumnName("ID");
            this.Property(t => t.Code).HasColumnName("Code");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.Description).HasColumnName("Description");
            this.Property(t => t.CreatedByID).HasColumnName("CreatedByID");
            this.Property(t => t.CreationDate).HasColumnName("CreationDate");
            this.Property(t => t.ModifiedByID).HasColumnName("ModifiedByID");
            this.Property(t => t.ModificationDate).HasColumnName("ModificationDate");
            this.Property(t => t.State).HasColumnName("State");
            this.Property(t => t.RevisionIndex).HasColumnName("RevisionIndex");

            HasRequired(t => t.CreatedBy).WithMany(a=>a.CreatedProjects).HasForeignKey(t=>t.CreatedByID);
            HasRequired(t => t.ModifiedBy).WithMany(a=>a.ModifiedProjects).HasForeignKey(t=>t.ModifiedByID);

my DAL repository looks like this:

 public ProjectRepository(ActivityTrackerDbContext context)
        {
            this.context = context;
            context.Configuration.LazyLoadingEnabled = false;
            context.Configuration.ProxyCreationEnabled = false;
        }

        public IEnumerable<Project> GetProjects()
        {
            var x= context.Projects.Include("CreatedBy").ToList();
            return x;
        }

Problem is that despite the fact that I have included only CreatedBy property, MOdifiedBy is also populated. What am I doing wrong or missing?

lazyLoading = false, navigation properties are virtual, but with loading of one property I am loading also another one...any help appriciated...

解决方案

Hello,

You might consider use a DTO (Data Transfer Object) class in tangent with AutoMapper.


这篇关于实体框架加载忽略include语句的所有相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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