实体框架中的实体状态和SaveChanges [英] Entity states and SaveChanges in Entity FrameWork

查看:76
本文介绍了实体框架中的实体状态和SaveChanges的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using (var context = new BloggingContext())
{
    var blog = new Blog { Name = "ADO.NET Blog" };
    context.Blogs.Add(blog);
    context.SaveChanges();
}



using (var context = new BloggingContext())
{
    var blog = new Blog { Name = "ADO.NET Blog" };
    context.Entry(blog).State = EntityState.Added;
    context.SaveChanges();
}



上面有什么区别,哪个会带来最佳性能

推荐答案

1.你应该使用第一种情况,因为代码更清晰!据我所知,他们之间在性能上没有区别。



2.通常,当您使用实体框架添加和/或修改对象时,您应该添加多个对象的添加和更改,并且只有在您使用它们进行中继时才为整个集合调用SaveCahnges();换句话说,如果可能的话,不应该对每个对象进行频繁的SaveChanges()调用,这将为您提供性能。
1.You should use the first case, because the code is more clear! As I know there is no difference between them regarding the performance.

2. In generally when you add and/or modify objects by using entity framework you should add multiple adding and changes of the objects and only when you relay finish with them to invoke "SaveCahnges()" for the entire set; in other words the "SaveChanges()" invocation should not be done to frequent for each object if is possible, and this will give you performance.


请访问以下链接

http://msdn.microsoft.com/en-in/data/jj592676.aspx [ ^ ]

这将为您提供有关它的完整信息。
Please visit Following Link
http://msdn.microsoft.com/en-in/data/jj592676.aspx[^]
this will gives you full information about it.


检查此示例应用程序:



初学者实施框架 [ ^ ]
Check this sample application:

Entity Framework for Beginners[^]


这篇关于实体框架中的实体状态和SaveChanges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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