Why can't I insert record with foreign key in a single server request?
- by Eran Betzalel
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();
}