InsertOnSubmit equivalent in DbContext.DbSet using Entity Framework 4 code first

后端 未结 2 581
情话喂你
情话喂你 2021-01-12 06:06

I am using entity framework code first approach, and I am building a generic Repository class that provides data access. In this class I want an Add(T entity) m

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 06:48

    I have literally just posted this question but I have found a way around the problem - use the Set(Type t) method instead of the generic version like so:

    public TEntity Add(TEntity entity)
    {
       return (TEntity)_database.Set(typeof(TEntity)).Add(entity);
    }
    

    A little bit of intellisense inspection goes a long way! Hope this helps someone...

提交回复
热议问题