简体   繁体   English

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

[英]How Do I Disable Lazy Loading, Entity Framework 4.1 using Code Migrations Configuration

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. 我也尝试过模型类的非虚拟属性,但这似乎完全没有区别。

I've always used 我一直都在用

context.Configuration.LazyLoadingEnabled = false;

calling it before using the DbContext methods, an equivalent setting is this: 在使用DbContext方法之前调用它,等效设置是这样的:

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

Max's solution isn't far from the point. Max的解决方案并不遥远。 Actually spurred me to look in a different location or the solution. 实际上促使我寻找不同的位置或解决方案。 Seems like you may be using EF Code First, yeah? 好像你可能正在使用EF Code First,是吗? So, in the Initialization of your context, there is the override of 'OnModelCreated'. 因此,在上下文的初始化中,存在“OnModelCreated”的覆盖。

In this method, I simply called up and set the property and all was resolved. 在这个方法中,我只是调用并设置属性,所有都已解决。

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

My model is now much more palatable. 我的模型现在更加可口。 Lazy loading is great...but not when you don't want it. 延迟加载很棒......但是当你不想要它时就不行了。 And when you start having circular references, it's just ridiculous. 当你开始使用循环引用时,这太荒谬了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架6:如何使用代码优先迁移和FTP部署重置数据库 - Entity Framework 6: How do I reset a database using code first migrations and an FTP deploy 实体框架:如何禁用特定查询的延迟加载? - Entity Framework: How to disable lazy loading for specific query? 如何在实体框架中进行迁移? - How do I make migrations in Entity Framework? 实体框架:如何在“代码优先”迁移中禁用模型兼容性检查 - Entity Framework: How to disable model compatibility check in Code First Migrations 延迟加载不使用Entity Framework 4.1的相关实体 - Lazy loading of related entities not working with Entity Framework 4.1 如果在实体框架中使用延迟加载,如何序列化实体? - How to serialize entity if using lazy loading in entity framework? 实体框架代码首次延迟加载 - Entity Framework Code First Lazy Loading 代码优先实体框架延迟加载不起作用 - Code First Entity Framework Lazy Loading Not Working 如何获得“实体框架代码优先”迁移来查看我的模型? - How do I get Entity Framework Code First Migrations to see my models? 如何使用Linq在Entity Framework 4.1中执行以下操作 - How to do following in Entity Framework 4.1 using Linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM