How to disable "lazy loading", "Entity Framework 4.1" using the configuration "Code Migration"

This is the im code used to configure the database:

 internal sealed class Configuration : DbMigrationsConfiguration<DataStore>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        SetSqlGenerator("System.Data.SqlServerCe.4.0", new SqlCeModelColumnBugWorkaroundProvider());

    }

    protected override void OnSeed(DbContext context)
    {
       context.Configuration.LazyLoadingEnabled = false;
       new SeedData(context as DataStore);
    }

    public static void DoDatabaseInitialisation()
    {
        var setting = ConfigurationManager.AppSettings["RequiresDbUpdate"];
        var requiresDbUpdate = bool.Parse(string.IsNullOrEmpty(setting) ? "false" : setting);

        if (! requiresDbUpdate) return;

        //otherwise create/update the database 
        var dbMigrator = new DbMigrator(new Configuration());
        dbMigrator.Update();

        ResetDbUpdateRequired("/");
    }

    private static void ResetDbUpdateRequired(string appPath)
    {
        var hostName = WebHelper.GetHost(false);

        if (!hostName.Contains("localhost"))
            WebHelper.UpdateWebConfigAppSetting("RequiresDbUpdate", "false", appPath);
    }

If anyone knows how to do this, let me know. I also tried non-virtual properties of the model classes, but this does not seem to make any difference.

+5
source share
2 answers

I always used

context.Configuration.LazyLoadingEnabled = false;

calling it before using the DbContext methods, the equivalent setting is this:

(context as IObjectContextAdapter).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
+3
source

. . , EF Code First, ? , "OnModelCreated".

, .

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
     base.Configuration.LazyLoadingEnabled = false;
}

. ... , . , .

+1

All Articles