Levenitz wrote an article titled "Entity Framework data insertion performance tracking". I felt good. At least he raised a question and wrote it out, which aroused discussion, this is an atmosphere. After reading the article + comments, I wrote a simple program and tried it.
Run the following code:
Two simple classes:
1: /// <summary>
2: // consumer
3: /// </summary>
4: public class Consumer
5: {
6: public int CId { get; set; }
7: public string CName { get; set; }
8: public List<Order> Orders { get; set; }
9: }
10:
11: /// <summary>
12: // order
13: /// </summary>
14: public class Order
15: {
16: public int OrderNo { get; set; }
17: public DateTime OrderDate { get; set; }
18: public decimal TotalMoney { get; set; }
19: public int CId { get; set; }
20:
21: public Consumer Consumer { get; set; }
22: }
Ing Configuration:
1: public class ConsumerConfiguration : EntityTypeConfiguration<Consumer>
2: {
3: public ConsumerConfiguration()
4: {
5: HasKey(t => t.CId).Property(t => t.CId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
6: Property(t => t.CName).IsRequired().HasMaxLength(50);
7: }
8: }
9:
10: public class OrderConfiguration : EntityTypeConfiguration<Order>
11: {
12: public OrderConfiguration()
13: {
14: HasKey(t => t.OrderNo);
15: HasRequired(t => t.Consumer).WithMany(t => t.Orders).HasForeignKey(t => t.CId);
16: }
17: }
Context:
1: public class TestContext : DbContext
2: {
3: public DbSet<Consumer> Consumers { get; set; }
4: public DbSet<Order> Orders { get; set; }
5:
6: protected override void OnModelCreating(DbModelBuilder modelBuilder)
7: {
8: modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
9:
10: modelBuilder.Configurations.Add(new ConsumerConfiguration());
11: modelBuilder.Configurations.Add(new OrderConfiguration());
12:
13: base.OnModelCreating(modelBuilder);
14: }
15: }
Test code:
1: static void Main(string[] args)
2: {
3: using (var ctx = new TestContext())
4: {
5: ctx.Consumers.Add(new Consumer()
6: {
7: cname = "James"
8: });
9: ctx.SaveChanges();
10:
11: Stopwatch sw = new Stopwatch();
12: sw.Start();
13: console. writeline ("order start: \ n ");
14:
15: for (int outer = 0; outer < 20000; outer++)
16: {
17: ctx.Orders.Add(new Order()
18: {
19: OrderDate = DateTime.Now,
20: TotalMoney = 100,
21: CId = 1,
22: });
23: }
24: ctx.SaveChanges();
25: sw.Stop();
26: console. writeline (SW. elapsed. minutes + "Minute" + Sw. elapsed. seconds + "second" + Sw. elapsed. milliseconds + "millisecond ");
27: }
28: }
The above code is the most common code, and there is nothing to explain. Focus on the content.
The environment where the above Code is run is vs2012 + SQL Server 2008 R2, and the machine configuration is 4 GB. The CPU usage is N years ago.
Run the above CodeVery slow, As levenitz said, it is time consuming to add data to context. The cause of this problem is that CTX is called every time. orders. before add (Order), EF will call detectchanges, which is explained on stackoverflow. The address is: interval Entity Framework dbcontext, which also introduces detectchange.
To solve the above slow speed problem, you can set
1: ctx.Configuration.AutoDetectChangesEnabled = false;
Next let's take a look at the execution speed after disabling:
Another solution is to use the dbset <t>. addrange method, which is added to 6.0 beta1.
1: List<Order> orderList = new List<Order>();
2: for (int outer = 0; outer < 20000; outer++)
3: {
4: orderList.Add(new Order()
5: {
6: OrderDate = DateTime.Now,
7: TotalMoney = 100,
8: CId = 1
9: });
10: }
11: ctx.Orders.AddRange(orderList);
12: ctx.SaveChanges();
In the system. Data. entity generic dbset class, the addrange method is a graph captured by reflector.
From the above two figures, we can see that both add and addrange are added to _ internalset. However, if autodetectchangesenabled is set to true, detectchanges is called before any entity is added. See the explanation in remarks.
Test Source: http://www.ef-community.com/forum.php? MoD = viewthread & tid = 437 & extra = Page % 3d1
Night is deep!
The college entrance examination is coming soon. I wish you all the candidates!
Technorati tags: EF, entityframework, detectchanges