When should I create a new DbContext()

前端 未结 5 1665
梦毁少年i
梦毁少年i 2020-12-02 06:25

I am currently using a DbContext similar to this:

namespace Models
{
    public class ContextDB: DbContext
    {

        public DbSet

        
5条回答
  •  攒了一身酷
    2020-12-02 07:18

    You should dispose the context immediately after each Save() operation. Otherwise each subsequent Save will take longer. I had an project that created and saved complex database entities in a cycle. To my surprise, the operation became three times faster after I moved "using (var ctx = new MyContext()){...}" inside the cycle.

提交回复
热议问题