Why can't I insert record with foreign key in a single server request?

Posted by Eran Betzalel on Stack Overflow See other posts from Stack Overflow or by Eran Betzalel
Published on 2010-05-24T08:06:56Z Indexed on 2010/05/24 8:41 UTC
Read the original article Hit count: 262

Filed under:
|
|

I'm tryring to do a simple insert with foreign key, but it seems that I need to use db.SaveChanges() for every record insert. How can I manage to use only one db.SaveChanges() at the end of this program?

var files = 
    new List<File>
        {
            new File { Name = "Test1" },
            new File { Name = "Test2" },
            new File { Name = "Test3" }
        };

foreach (var file in files)
{
    db.AddToFileSet(file);
    db.SaveChanges();

    db.AddToDirectorySet(
        new Directory
        {
            DirectoryName = file.Name + "Dir",
            CreationDate = DateTime.UtcNow,
            file_relation = file
        });
    db.SaveChanges();
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .net-3.5