开发者

Is it possible to insert multiple child records using the entity framework?

开发者 https://www.devze.com 2023-03-28 05:03 出处:网络
Using entity framework.Is it possible to insert multiple child records.At the moment I am iterating through a list and adding to entity object, but only one/the last object is saved to the database.Sh

Using entity framework. Is it possible to insert multiple child records. At the moment I am iterating through a list and adding to entity object, but only one/the last object is saved to the database. Should I just create a sproc, or is this possible with EF? e.g.

            //dc = datacontext
            var fileList = Session["FileNames"];
            string[] sA = fileList.ToString().Split('|');
            for (int i = 0; i < sA.Count(); i++)
            {                  
                wcc.ID = id; //which has been supplied through a previous 
          //dc.SaveChanges
                wcc.FileName = sA[i];
                dc.AddTotbObject(wcc);                   
            }
            dc.SaveChanges();
            dc.Dispose(开发者_StackOverflow中文版);


You're only ever adding a single instance. You'll need to do something along these lines within your loop:

wcc = new tbObject();
wcc.ID = id;
wcc.FileName = sA[i];
dc.AddTotbObject(wcc);
0

精彩评论

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