Mocking or faking DbEntityEntry or creating a new DbEntityEntry

后端 未结 2 637
甜味超标
甜味超标 2020-12-01 04:12

Following on the heels of my other question about mocking DbContext.Set I\'ve got another question about mocking EF Code First.

I now have a method for my update tha

2条回答
  •  感情败类
    2020-12-01 04:53

    Just like the other case, what you need is to add an additional level of indirection:

    interface ISalesContext
    {
        IDbSet GetIDbSet();
        void SetModified(object entity)
    }
    
    class SalesContext : DbContext, ISalesContext
    {
        public IDbSet GetIDbSet()
        {
            return Set();
        }
    
        public void SetModified(object entity)
        {
            Entry(entity).State = EntityState.Modified;
        }
    }
    

    So, instead of calling the implementation, you just call SetModified.

提交回复
热议问题