如何禁用延迟加载,实体框架4.1使用代码迁移配置 [英] How Do I Disable Lazy Loading, Entity Framework 4.1 using Code Migrations Configuration

查看:93
本文介绍了如何禁用延迟加载,实体框架4.1使用代码迁移配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the code im using 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 anybody knows how to do this, please let me know. I have also tried non-virtual properties on the model classes but this seems to make no difference at all.

推荐答案

我已经总是用

context.Configuration.LazyLoadingEnabled = false;



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

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

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

这篇关于如何禁用延迟加载,实体框架4.1使用代码迁移配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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