I am writing code to access a database using EntityFrameWork. The code:
public IEnumerable<Rows> GetRows(int id) { using (var context = new ApplicationDbContext()) { var repository = new EntityFrameWorkRepository<int, RowEntity>(context);
GetRowsFromDb () uses LINQ to query the database and filter the results using an identifier.
I originally wrote this method without calling ToList (), but when I tried to access the objects in the returned IEnumerable, I would get an exception from an existing dbcontext. I do not understand how this code fixes everything, although it works. I believe that ToList () is a deep copy of the object and that it possibly provides the necessary separation from context / database, but, of course, should the original object be useful?
c # linq entity-framework asp.net-mvc-5
CalumMcCall
source share