Comments (12)
Hey, I cant work this around... Something changed?
from entityframework-classic.
Hello @pilarodriguez ,
Thank you for reporting, we will look at it today.
Best Regards,
Jonathan
from entityframework-classic.
Hello @pilarodriguez ,
That one has been a lot hard then we initially thought but we finally make it compatible with Moq.
The v7.0.24 has been released.
Let me know if everything works as expected.
If you need, we can also provide an example that works with Include
.
Best Regards,
Jonathan
from entityframework-classic.
We will look at it today
from entityframework-classic.
Thank you. Yes, it would be great it you could provide me some examples for Include
from entityframework-classic.
Sure, you should get it later today.
from entityframework-classic.
Hello @pilarodriguez ,
Here is an example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Castle.Core.Logging;
using Moq;
namespace Z.Lab.Request_Mock
{
public partial class Form_Request_QueryFilter_Interface : Form
{
public static int ApplicationTenantID = 1;
public Form_Request_QueryFilter_Interface()
{
var customer = new Customer();
var order = new Order() { Name = "blabla" };
customer.Orders.Add(order);
var customers = new List<Customer>
{
customer
}.AsQueryable();
var orders = new List<Order>
{
order
}.AsQueryable();
var mockContext = new Mock<EntityContext>(MockBehavior.Loose);
var mockCustomersSet = new Mock<DbSet<Customer>>();
mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.Provider).Returns(customers.Provider);
mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.Expression).Returns(customers.Expression);
mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.ElementType).Returns(customers.ElementType);
mockCustomersSet.As<IQueryable<Customer>>().Setup(m => m.GetEnumerator()).Returns(customers.GetEnumerator());
mockCustomersSet.Setup(m => m.Include("Orders")).Returns(mockCustomersSet.Object);
var mockOrdersSet = new Mock<DbSet<Order>>();
mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.Provider).Returns(orders.Provider);
mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.Expression).Returns(orders.Expression);
mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.ElementType).Returns(orders.ElementType);
mockOrdersSet.As<IQueryable<Order>>().Setup(m => m.GetEnumerator()).Returns(orders.GetEnumerator());
mockContext.SetupGet(c => c.Customers).Returns(mockCustomersSet.Object);
mockContext.SetupGet(c => c.Orders).Returns(mockOrdersSet.Object);
var customerList = mockContext.Object.Customers.Include("Orders").Select(p => p).ToList();
}
public class EntityContext : DbContext
{
public EntityContext() : base(My.ConnectionString)
{
}
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Order> Orders { get; set; }
}
public class Customer
{
public Customer()
{
Orders = new List<Order>();
}
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsDeleted { get; set; }
public virtual List<Order> Orders { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsDeleted { get; set; }
}
}
}
Let me know if you need more help on this issue.
Best Regards,
Jonathan
from entityframework-classic.
Hello @JonathanMagnan . Sorry, I was having some other issues with my code and I couldn't try the new package version.
The problem I have with your example is that we are not using dbContext.Customers.Include("Orders")
. We are using dbContext.Customers.Include(s => s.Orders)
, which uses the public IncludeDbQuery<TResult, TProperty> Include<TProperty>(Expression<Func<TResult, TProperty>> path);
.
So I cannot mock it with mockCustomersSet.Setup(m => m.Include("Orders")).Returns(mockCustomersSet.Object);
from entityframework-classic.
Hello @pilarodriguez ,
My developer tried it and it seems to works with both syntax.
We tried it with var customerList = mockContext.Object.Customers.Include(p => p.Orders).Select(p => p).ToList();
as you use and everything work.
Are we missing something in your last comment?
from entityframework-classic.
I finally have it working. The problem is that instead of var customerList = mockContext.Object.Customers.Include(p => p.Orders).Select(p => p).ToList();
, I was trying something like var customerList = mockContext.Object.Customers.Include(p => p.Orders).ToList();
(without the Select), so I was getting an exception.
I'm not sure why I have a problem if I just do a ToList after the Include
from entityframework-classic.
Hello @pilarodriguez ,
We tried with the following code:
var customerList = mockContext.Object.Customers.Include("Orders").ToList();
And that still work for us, the Select
was only here by mistake.
If everything works, could we close this request?
Best Regards,
Jonathan
from entityframework-classic.
Sure. Thanks a lot for your support
from entityframework-classic.
Related Issues (20)
- Type not supported yet (LazyType): sdo_geometry HOT 11
- "Cannot bind argument to parameter 'Path' because it is an empty string" when running Add-Migration HOT 9
- Include with InvalidCastException exception HOT 4
- IDbContext HOT 8
- EF6 is available for .NET Core 3+ HOT 1
- HasNoKey and Constructor with Parameters HOT 5
- Make DbMigrator constructors public HOT 4
- Link to entityframework.net HOT 3
- Count() inside string expression in Execute method HOT 4
- Creating new array inside expression string - Execute extension method HOT 8
- Error when using UpdateFromQuery on an expanded query (Linqkit) HOT 3
- DbMigrator - Problems with existing migrations HOT 6
- HierarchyId.GetAncestor method raise an unexpected exception HOT 7
- Current Values are not reset when using SoftDelete HOT 6
- this.Configuration not availalbe in .net core 6 for soft delete HOT 3
- EDMX - Unable to cast object of type ObjectQuery to DbQuery in .Include() HOT 19
- The object cannot be deleted because it was not found in the ObjectStateManager HOT 1
- IDbCommandInterceptor called multiple times on queries with .IncludeOptimizedByPath() HOT 2
- add new provider Microsoft.Data.SqlClient HOT 3
- Unable to load the specified metadata resource. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework-classic.