Entity Framework 7 Create in-memory implementation of DbSet

asp.net c# entity-framework entity-framework-core

Question

Sorry I am new to Entity Framework..

I want to created a TestDbSet that provides an in-memory implementation of DbSet in EF 7.

public class TestDbSet<TEntity> : DbSet<TEntity>, IQueryable, IEnumerable<TEntity>, IDbAsyncEnumerable<TEntity> 
        where TEntity : class 
    { 
        ObservableCollection<TEntity> _data; 
        IQueryable _query; 


    public TestDbSet() 
    { 
        _data = new ObservableCollection<TEntity>(); 
        _query = _data.AsQueryable(); 
    } 

    public override TEntity Add(TEntity item) 
    { 
        _data.Add(item); 
        return item; 
    } 

    public override TEntity Remove(TEntity item) 
    { 
        _data.Remove(item); 
        return item; 
        } 
 }

I am getting error in Add method that return type must be EntityEntry<T> to match Overriden Dbset member.

I tried to change method signature but I am not sure how to return item as EntityEntry

 public override EntityEntry<T> Add(T item) 
        {
           _data.Add(item);
            return  item;
        }

I could not find similar issue. Can you guide me how we implement in EF 7.. I know it will work in EF 6..

1
3
12/12/2016 2:18:09 AM

Popular Answer

For testing without real DB you may use Microsoft.EntityFrameworkCore.InMemory package

3
12/11/2016 10:33:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow