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

后端 未结 2 582
情话喂你
情话喂你 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:38

    Add a generic constraint to your repository class:

    public class Repository<TEntity> where TEntity : class
    
    0 讨论(0)
  • 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...

    0 讨论(0)
提交回复
热议问题