dbset

How do I mock DbContext using NSubstitute and then add/remove data

↘锁芯ラ 提交于 2020-05-12 15:42:20
问题 I need to mock EF's DbContext . I use the approach here and it works well. // mock a DbSet var mockDbSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>(); var data = new List<Foo>().AsQueryable(); ((IQueryable<Foo>)mockDbSet).Provider.Returns(data.Provider); ((IQueryable<Foo>)mockDbSet).Expression.Returns(data.Expression); ((IQueryable<Foo>)mockDbSet).ElementType.Returns(data.ElementType); ((IQueryable<Foo>)mockDbSet).GetEnumerator().Returns(data.GetEnumerator()); // now add it to a mock

Perfomance issue - .Count in MVC view

萝らか妹 提交于 2020-03-06 07:35:31
问题 For several pages I use a .Count inside a foreach loop @model ICollection<Item> foreach(var item in Model) { @item.Flight.FlightReservations.count } Because of lazy loading the EF makes a round trip to the database for this. Now I want to fix this by using this or linq version: Include("List.Flight.FlightReservations") Doing this makes loading my dbSet take even longer than those foreach round trips How can I 'load' parts of only 1 object? I would like to use context.Items.Single(p => p.id ==

Find a specified generic DbSet in a DbContext dynamically when I have an entity

若如初见. 提交于 2020-01-30 19:36:49
问题 I have following classes and DbContext : public class Order:BaseEntity { public Number {get; set;} } Product:BaseEntity; { public Name {get; set;} } public class Context : DbContext { .... public DbSet<Order> Orders { set; get; } public DbSet<Product> Products { set; get; } .... } I have a list of objects that want to add to my context, too, but I don't know how can I find appropriate generic DbSet according each entity type dynamically. IList<BaseEntity> list = new List<BaseEntity>(); Order

Moq DbSet NotImplementedException

送分小仙女□ 提交于 2019-12-30 23:46:39
问题 I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m

Moq DbSet NotImplementedException

孤人 提交于 2019-12-30 23:46:32
问题 I have a Moq DbSet that has been working until recently, however since the last update of dependencies it keeps throwing a NotImplementedException on IQueryable.Provider Code used as follows: var mockSet = new Mock<DbSet<A>>(); var list = new List<A>(); var queryable = list.AsQueryable(); mockSet.As<IQueryable<A>>().Setup(m => m.Provider).Returns(queryable.Provider); mockSet.As<IQueryable<A>>().Setup(m => m.Expression).Returns(queryable.Expression); mockSet.As<IQueryable<A>>().Setup(m => m

Converting an IQueryable<T> to a DbSet<T>

坚强是说给别人听的谎言 提交于 2019-12-23 09:08:58
问题 I'm not sure that this is possible, but I'm trying to unit test a repository that uses a DbSet. I thought the easiest solution would just be to make an Enumerable, and replace the DbSet with that, this is my attempt. I'm using C#, EntityFramework, XUnit, and Moq [Fact] public void SomeTest() { //arrange var mockContext = new Mock<MyDbContext>(); var mockData = new List<Person> { new Person { Name = "Jim", Age = 47 } }; mockContext.Setup(db => db.Persons).Returns((DbSet<Person>)mockData

How DbContext initializes automatic DbSet<T> properties?

若如初见. 提交于 2019-12-20 01:43:41
问题 Consider the following class: class MyContext : DbContext { public DbSet<Order> Orders { get; set; } } and instantiating a new object: var mycontext = new MyContext(); Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object. 回答1: From looking at the reflected code, when the DbContext

Create a DbSet<T> dynamically in Entity Framework?

*爱你&永不变心* 提交于 2019-12-18 12:09:51
问题 In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable<T> . 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 { public DbSet<MySet> MySets {get; set;} } I would like to know how can I create/get a reference to MySets dynamically as I can with LINQ to SQL as in: var mySet = MyDbContext.GetTable<MySet>(); 回答1: DbContext has method for this: var set = context

How to add an item to a Mock DbSet (using Moq)

二次信任 提交于 2019-12-18 10:12:36
问题 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 problem i was having). However, I am having difficulty adding items to the DbSet. The output is preCount = 3 postCount = 3. However, I expect it to be precount = 3 postCount = 4. Any help is greatly appreciated. static void Main(string[] args) { Debug

Looping over derived DbContext DbSets so I can add a ClearAll() extension to my DbContext object

a 夏天 提交于 2019-12-17 21:17:56
问题 Based on [the difference between Type.IsGenericType and Type.IsGenericTypeDefinition ][1], I gained the piece of information I was missing in order to be able to retrieve a DbContext's DbSets via Reflection. However, I am now stuck somewhere else. In the code sample below, I do succeed in obtaining the DbSet generic instances, but I now want to be able to cast them at compile time, to the specific generic type defintion. dbSet is a real instance of one of the DbSets<...> from my DbContext,