Entity framework 6 Lazy loading with db initiation

.net c# entity-framework entity-framework-6 lazy-loading

Question

I am using Lazy loading with EF 6.

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 remove the sluggish loading setting,

    Configuration.LazyLoadingEnabled = false; 

It works fine. There are no loaded relational objects.item.SecondProgram is null . Perfect. However, my database initialization creates a new database when I remove the existing one, and subsequent sluggish loading is ineffective. Lazy loading resumes working properly if I run the aforementioned software one more. Do you folks understand why? Thanks.

Here is my builder.

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. Lazy loading works if I build a new context object and use it to query the database.

Update -

My whole console programme is below. Run it and see whether the item's SecondProgram attribute exists. It is not empty. There, lazy loading did not operate. Run the application once again without erasing the database, and then verify the SecondProgram property once more. Is is, as anticipated, nil. Why wasn't it 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; }
}
1
1
8/27/2016 1:37:28 PM

Popular Answer

ZZZ_tmp
1
8/27/2016 2:21:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow