实体框架6 Lazy加载db启动 [英] Entity framework 6 Lazy loading with db initiation

查看:126
本文介绍了实体框架6 Lazy加载db启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF 6和Lazy加载。

  class MainPrograme 
{
static void Main(string [] args)
{
ProgramContext _dbContext = new ProgramContext();

_dbContext.Programs.Add(new Program
{
SecondProgram = new SecondProgram
{
Title =Demo
}
});
_dbContext.SaveChanges();
var item = _dbContext.Programs.Find(1);
}
}

一旦我禁用延迟加载

  Configuration.LazyLoadingEnabled = false; 

它工作正常。没有加载关系对象。 item.SecondProgram null 。完善。但是,当我删除数据库时,我的db启动会设置一个新的db,然后延迟加载不起作用。如果我再次运行上述程序,那么延迟加载工作再次正常。你们有什么想法吗谢谢。



这是我的构造函数

  public ProgramContext()
:base(Data Source = xxx;)
{
Configuration.LazyLoadingEnabled = false;
if(!Database.Exists())
{
Task.Run(InitializeDatabase).Wait();
}
}

如果数据库不存在,InitializeDatabase将设置db如果我在同一上下文实例中执行数据库查询,则延迟加载失败。如果我创建一个新的上下文对象并通过这个查询数据库,懒惰的加载工作。



更新 -



以下是我的完整控制台程序。运行它并检查SecondProgram属性如果项目。它不为空懒惰加载没有在那里工作然后再次运行程序,而不删除数据库,并再次检查SecondProgram属性。 Is是如预期的null。为什么在第一次运行时不为空?

  class MainPrograme 
{
static void Main [] args)
{
ProgramContext _dbContext = new ProgramContext();

_dbContext.Programs.Add(new Program
{
SecondProgram = new SecondProgram
{
Title =Demo
}
});
_dbContext.SaveChanges();
var item = _dbContext.Programs.Find(1);
}
}

public class Program
{
public int Id {get;组; }
public string标题{get;组; }

public virtual ICollection< SecondProgram> SecondPrograms {get;组; }
public virtual SecondProgram SecondProgram {get;组; }
}

public class SecondProgram
{
public int Id {get;组; }
public string标题{get;组;

}

public class ProgramContext:DbContext
{
public ProgramContext()
:base(Data Source = XXX; Initial Catalog = MyContainer; Integrated Security = True;)
{
Configuration.LazyLoadingEnabled = false;
}

public DbSet< Program>节目{get;组; }
}


解决方案

在上下文中通过在配置属性上设置标志来关闭所有实体,如下所示。请检查您是否已完成。



示例:

  public class MyContext:DbContext 
{
public MyContext()//构造函数
{
this.Configuration.LazyLoadingEnabled = false;
}
}

注意:仍然使用



更新:



可以使用这个:

  Database.SetInitializer< ProgramContext>(new CreateDatabaseIfNotExists< ProgramContext>()); 

而不是你的代码:

  if(!Database.Exists())
{
Task.Run(InitializeDatabase).Wait();
}

更新2:



您的问题是您没有在配置属性中使用这个关键字你必须如下所示设置。

  this.Configuration.LazyLoadingEnabled = false; 


I am working with EF 6 and Lazy loading.

class MainPrograme
{
    static void Main(string[] args)
    {
        ProgramContext _dbContext = new ProgramContext();

        _dbContext.Programs.Add(new Program
        {
            SecondProgram = new SecondProgram
            {
                Title = "Demo"
            }
        });
        _dbContext.SaveChanges();
        var item = _dbContext.Programs.Find(1);
    }
}

Once I disable lazy loading with

    Configuration.LazyLoadingEnabled = false; 

It works fine. No relational objects are loaded. item.SecondProgram is null. Perfect. However when I delete the database, my db initiation sets up a new db and then lazy loading is not working. If I run the above program again, then lazy loading works fine again. Do you guys have any idea why ? Thanks.

Here is my constructor

public ProgramContext()
        : base("Data Source=xxx;")
    {
        Configuration.LazyLoadingEnabled = false;
        if (!Database.Exists())
        {
            Task.Run(InitializeDatabase).Wait();
        }
    }

If database does not exist, InitializeDatabase will setup the db and if I do a db query in the same context instance, lazy loading fails. If I create a new context object and query db via that, lazy loading works.

Update -

Below is my full console program. Run it and check SecondProgram property if the item. It is not null. Lazy loading not worked there. Then run the program again without deleting the database and check SecondProgram property again. Is is null as expected. Why is was not null in the first run ?

class MainPrograme
{
    static void Main(string[] args)
    {
        ProgramContext _dbContext = new ProgramContext();

        _dbContext.Programs.Add(new Program
        {
            SecondProgram = new SecondProgram
            {
                Title = "Demo"
            }
        });
        _dbContext.SaveChanges();
        var item = _dbContext.Programs.Find(1);
    }
}

public class Program
{
    public int Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<SecondProgram> SecondPrograms { get; set; }
    public virtual SecondProgram SecondProgram { get; set; }
}

public class SecondProgram
{
    public int Id { get; set; }
    public string Title { get; set; }

}

public class ProgramContext : DbContext
{
    public ProgramContext()
        : base("Data Source=XXX;Initial Catalog=MyContainer;Integrated Security=True;")
    {
        Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Program> Programs { get; set; }
}

解决方案

Lazy loading can be turned off for all entities in the context by setting a flag on the Configuration property as shown below.Please check whether you have done that or not.

example:

 public class MyContext : DbContext 
    { 
        public MyContext() //constructor
        { 
            this.Configuration.LazyLoadingEnabled = false; 
        } 
    }

Note : You can still Load related entities by using Eager Loading

Update :

can you use this :

Database.SetInitializer<ProgramContext>(new CreateDatabaseIfNotExists<ProgramContext>());

Instead of your code :

 if (!Database.Exists())
    {
      Task.Run(InitializeDatabase).Wait();
    }

Update 2 :

Your problem here is you haven't used this keyword with the Configuration property.You have to set it as shown below.

 this.Configuration.LazyLoadingEnabled = false; 

这篇关于实体框架6 Lazy加载db启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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