开发者

Custom InserAllOnSubmit in Entity Framework

i have just shifted from L2S to EF 4. In L2S, I heavily used InsertAllOnSubmit method of datacontext but in EF, it seems no such method is available. i have written this generic extension method on ObjectContext

public static void InsertAllOnSubmit<T>(this ObjectContext db, List<T> newentities) where T:EntityObject
        {
          开发者_如何学Go  var objectSet = db.CreateObjectSet<T>();
            newentities.ForEach(x => objectset.AddObject(x));
        }

i want to know what are the pitfalls of using this method. One that i can guess is that there is some cast associated with CreateObjectSet<T>(). But, i have no idea how much cost is incurred in creating objectset plus i want to know what other ill effects this method will have

thanks


When it comes to performance questions like this the only way you are going to really determine the cost is by doing a test. I could be wrong, but I bet the Linq to Sql call InsertAllOnSubmit is doing something like you are doing as far as a foreach loop, adding each entity one at a time. Only way to tell that is to pull up something like reflector to look into the code of the .net framework.

The only real way you are going to determine the costs of a given piece of code is to do some tests. If you are worried about the cost of db.CreateObjectSet, try creating another method that utilizes you actual EF DataContext and run both through some stress tests. That will let you know how expensive your above method is.


Not sure why you are trying to rewrite entity framework this is already supported it is done in a different way in ef. in ef you can simply add the objects to the record collection and call SaveChanges() the framework will save any changes including edits inserts and deletes when this is called

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜