Questions tagged [dbset]
224 questions
77
votes
2 answers
How to add an item to a Mock DbSet (using Moq)
I'm trying to set up a mock DbSet for testing purposes. I used the tutorial here, http://www.loganfranken.com/blog/517/mocking-dbset-queries-in-ef6/ and slightly modified it so calling GetEnumerator returns a new enumerator each time (another…

user1080952
- 1,073
- 2
- 10
- 15
52
votes
6 answers
Find a specified generic DbSet in a DbContext dynamically when I have an entity
I have following classes and DbContext:
public class Order : BaseEntity
{
public Number {get; set;}
}
public class Product : BaseEntity;
{
public Name {get; set;}
}
public class Context : DbContext
{
....
public DbSet Orders…

Masoud
- 8,020
- 12
- 62
- 123
44
votes
5 answers
Can you get the DbContext from a DbSet?
In my application it is sometimes necessary to save 10,000 or more rows to the database in one operation. I've found that simply iterating and adding each item one at a time can take upwards of half an hour.
However, if I disable…

berkeleybross
- 1,314
- 2
- 13
- 27
43
votes
3 answers
Get all rows using entity framework dbset
I want to select all rows from a table using the following type of syntax:
public IQueryable GetCompanies()
{
return DbContext.Set()
.// Select all
}
Forgive me as I am completely new to EF.

Wesley Skeen
- 7,977
- 13
- 42
- 56
42
votes
7 answers
NSubstitute DbSet / IQueryable
So EntityFramework 6 is a lot better testable then previous versions. And there are some nice examples on the internet for frameworks like Moq, but the case is, I prefer using NSubstitute. I've got the "non-query" examples translated to work with…

s.meijer
- 3,403
- 3
- 26
- 23
37
votes
1 answer
DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID
I have the following code (Database is SQL Server Compact 4.0):
Dim competitor=context.Competitors.Find(id)
When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records.
When I change the code to:
Dim…

Dabblernl
- 15,831
- 18
- 96
- 148
30
votes
3 answers
Create a DbSet dynamically in Entity Framework?
In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable. Is there a similar way to do this in Entity Framework 4 other than declaring the properties on the specific DbContext? For example:
public MyDbContext: DbContext
{
…

skjagini
- 3,142
- 5
- 34
- 63
19
votes
4 answers
Is DbSet<>.Local something to use with special care?
For a few days now, I have been struggling with retrieving my entities from a repository (DbContext).
I am trying to save all the entities in an atomic action. Thus, different entities together represent something of value to me. If all the…

bas
- 13,550
- 20
- 69
- 146
12
votes
1 answer
Entity Framework dbset most efficient way of deleting
I have the following and looking for a more efficient way of deleting vs looping through the records and then deleting each one at a time (note using Dbset):
var wcd = dbContext.ProgramDetails.Where(p => p.Id == Id);
foreach (var wc in…

Nate Pet
- 44,246
- 124
- 269
- 414
9
votes
0 answers
How to create a custom DbSet
How to create a custom DbSet to be used in our context,
here I ferived from DbSet which the code sample is provided below.
In the Context I have:
public CustomDbSet MyDbSet { get; set; }
any time we reach "context.MyDbSet"
it's…

LastBye
- 1,143
- 4
- 19
- 37
9
votes
4 answers
DbSet.Cast() Error: Cannot create a DbSet from a non-generic DbSet for objects of type 'Entity'
Version Info:
I am using C# 4.5, Entity Framework 6.0, and MEF.
Code and Unit Test
I created a Test Project to explain the problem:
https://skydrive.live.com/redir?resid=E3C97EC293A34048!2234
Please Open the UnitTest project and try to run…

Aidin
- 2,134
- 22
- 26
8
votes
2 answers
Apply OrderBy with DbSet
I am trying to implement pagination and sorting with generic repository. How to take primary key column as default order by column in DbSet ?
DbSet = Context.Set();
public IQueryable GetAll(int pageNumber = 0, int pageSize = 10, string…
user1618825
8
votes
1 answer
Does converting from DbSet to IEnumerable cause the query to execute?
I have the following 2 methods in my Logs repository.
public IEnumerable GetAll()
{
var db = new CasLogEntities();
return db.Logs;
}
public DbSet GetAllSet()
{
var db = new CasLogEntities();
return db.Logs;
} …

David Kethel
- 2,440
- 9
- 29
- 49
7
votes
2 answers
Dynamic DbSet in Entity Framework Core
string tableName = "TblStudents";
Dictionary myDictionary = new Dictionary()
{
{ "TblStudents", typeof(TblStudent) },
{ "TblTeachers", typeof(TblTeacher) }
};
// Context always same
DBContext dbContext = new…

bbusdriver
- 1,577
- 3
- 26
- 58
7
votes
2 answers
How do I mock DbContext using NSubstitute and then add/remove data
I need to mock EF's DbContext. I use the approach here and it works well.
// mock a DbSet
var mockDbSet = Substitute.For, IQueryable>();
var data = new…

h bob
- 3,610
- 3
- 35
- 51