[英]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.