AutoDetectChangesEnabled的目的是什么? [英] What is the purpose of AutoDetectChangesEnabled?
问题描述
我知道如果AutoDetectChangesEnabled设置为true,那么在几个其他方法调用(Find / Add / Remove / SaveChanges等)中会自动调用DetectChanges但我不明白
为什么?
我通过添加"AutoDetectChangesEnabled = false"修改了一些简单代码的第一个示例(可在各种博客中找到)。在我的 DbContext
的构造函数中,所有这些示例仍然有效。关闭AutoDetectChangesEnabled标志似乎没有改变任何东西(它实际上使程序运行得更快)。
下面更具体的是我的"测试"的完整示例:很多与AutoDetectChangesEnabled = false完美配合的多个示例。设置更改不会影响程序。
任何人都可以解释或给我一个例子(或更改下面的示例),这样我就可以看到AutoDetectChangesEnabled标志何时实际执行某些操作(更改程序的行为)?
使用  ;系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Data.Entity;
命名空间 ReallyEasyCodeFirst
{
public class 代码
  ; {
public int TagId { get ; set ; }
public string 输入 { < span style ="color:blue"> get ; set ; }
public virtual ICollection < 发布> 帖子 { get ; set ; }
}
< span style ="color:blue"> public class 发布
{
public int PostId { get ; set ;  ;}
public DateTime 日期 { 获取; 设置; }
  ; public virtual ICollection < 标签> 标签 { 获取; 设置; }
}
public class MyContext : DbContext
{
public MyContext() : base ()
{
Configuration.AutoDetectChangesEnabled = false < /跨度>;
}
public DbSet < 标签> 标签 { get ; set ; }
public DbSet < 帖子> 帖子 { get ; set ; }
}
class 计划
{
static < span style ="color:blue"> void Main( string [] args)
  ;&NBSP;&NB sp; {
using ( var context = new MyContext () )
{
var p1 = new 发布 { Date = DateTime 。现在, 标签 = 新 列表< 标记>() };
var p2 = new 发布 { Date = 日期时间。现在, 标签 = new 列表< 标签>() };
var t1 = new 标记 { Type = " Type1" , 帖子 = new 列表< 发布> { p1 } };
var t2 = new 标记 { Type = " Type2" , 帖子 = new 列表< 发布> { p1, p2 } };
var t3 = new 标记 { Type = " Type3" , 帖子 = new 列表< 发布> {p2} };
context.Tags.Add(t1);
context.Tags.Add(t2);
context.Tags.Add(t3);
Console .WriteLine(p1.Tags 。计数);
Console .WriteLine(p2.Tags 。计数);
context.SaveChanges();
}
使用 ( var context = new MyContext ())
{
var p1 = context.Posts.Find( 1);
控制台
.WriteLine(p1.Tags.Count);
}
}
}
}
Hello LambdaCruiser,
欢迎来到EF论坛。
之前我已经回答了一个帖子,这使我跟踪了AutoDetectChangesEnabled为真或假的影响。我在这里引用一些:
让我们跟踪DetectChanges()方法如下:
public
< span style ="color:#1000a0"> virtual void DetectChanges (bool force =
false )
{
if ( this .AutoDetectChangesEnabled || force)
&NBSP;&NBSP;&NBSP; {
this .ObjectContext.DetectChanges();
}
}
在DetectChanges()方法中,我们可以看到代码调用了ObjectContext.DetectChanges()方法,因此对象的状态将会改变。
更多信息,你可以看到这个帖子:
< p style ="margin:0cm 0cm 10pt"> 我希望这可以帮到你。如果您有任何疑问,请随时告诉我。
祝你有个愉快的一天,
I know that if AutoDetectChangesEnabled is set to true then DetectChanges will be called automatically during several other methods calls (Find/Add/Remove/SaveChanges and more) but i don't understand WHY?
I modified few simple code first examples (found on various blogs) by adding "AutoDetectChangesEnabled=false" in the constructor of my DbContext and all those examples still work. Turning off the AutoDetectChangesEnabled flag seemingly didn't change anything (it actually made the programs run faster).
To be more concrete below is a full example of my "test": a many-to-many example that works perfectly well with AutoDetectChangesEnabled=false. The setting change didn't affect the program. Can anyone explain or give me an example (or change the example below) so I can see when AutoDetectChangesEnabled flag actually does something (changes the behaviour of the program)?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; namespace ReallyEasyCodeFirst { public class Tag { public int TagId { get; set; } public string Type { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public DateTime Date { get; set; } public virtual ICollection<Tag> Tags { get; set; } } public class MyContext : DbContext { public MyContext() : base() { Configuration.AutoDetectChangesEnabled = false; } public DbSet<Tag> Tags { get; set; } public DbSet<Post> Posts { get; set; } } class Program { static void Main(string[] args) { using (var context = new MyContext()) { var p1 = new Post { Date = DateTime.Now, Tags = new List<Tag>() }; var p2 = new Post { Date = DateTime.Now, Tags = new List<Tag>() }; var t1 = new Tag { Type = "Type1", Posts = new List<Post>{ p1 } }; var t2 = new Tag { Type = "Type2", Posts = new List<Post> { p1, p2 } }; var t3 = new Tag { Type = "Type3", Posts = new List<Post>{p2} }; context.Tags.Add(t1); context.Tags.Add(t2); context.Tags.Add(t3); Console.WriteLine(p1.Tags.Count); Console.WriteLine(p2.Tags.Count); context.SaveChanges(); } using (var context = new MyContext()) { var p1 = context.Posts.Find(1); Console
.WriteLine(p1.Tags.Count); } }
} }
Hello LambdaCruiser,
Welcome to the EF Forum.
I have answered a thread before, which make me tracking the influence where AutoDetectChangesEnabled is true or false. I quote some here:
Let’s keep tracking the DetectChanges() method as following:
public virtual void DetectChanges(bool force = false)
{
if (this.AutoDetectChangesEnabled || force)
{
this.ObjectContext.DetectChanges ();
}
}
In the DetectChanges() method, we can see that the code calls ObjectContext.DetectChanges() method, so the state of object will be changed.
More information, you could see this thread:
I hope this can help you. If you have any questions, please feel free to let me know.
Have a nice day,
这篇关于AutoDetectChangesEnabled的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!