Multiple contexts per thread
Is it safe to create multiple Context in one thread to save the data in the web application ? I've got the code like below and worry if it would work on big amount of iterations. Is there any problems with such code ?
private void SaveContent(TargetSegment ts,
Folder f,
IEnumerable<Module> m)
{
using(var ctx = new EntityContainer())
{
var entity = CreateEntityFrom(ts, 开发者_JS百科f, m);
ctx.Entities.Add(entity);
ctx.SaveChanges();
}
}
foreach(var ts in TargetSegments)
{
var f = FindFolder(ts);
var m = ExtractModules(ts, f);
SaveContent(ts, f, m);
}
There is no problem with your code. This above code will execute a SQL INSERT statement each time it is called. You have your code in a using block, which is the best way to dispose of the object.
I've run into performance issues only when selecting from an EF model for multiple transactions; and in that case, I cached whenever possible and have had no problems.
精彩评论