如何使用代码迁移配置禁用延迟加载,实体框架4.1

woz*_*woz 5 c# entity-framework-4.1

这是用于配置数据库的代码:

 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);
    }
Run Code Online (Sandbox Code Playgroud)

如果有人知道怎么做,请告诉我.我也尝试过模型类的非虚拟属性,但这似乎完全没有区别.

Mas*_*ini 3

我一直用

context.Configuration.LazyLoadingEnabled = false;
Run Code Online (Sandbox Code Playgroud)

在使用 DbContext 方法之前调用它,等效的设置是这样的:

(context as IObjectContextAdapter).ObjectContext.ContextOptions.LazyLoadingEnabled = false;
Run Code Online (Sandbox Code Playgroud)