Giter Site home page Giter Site logo

Comments (5)

fiseni avatar fiseni commented on May 25, 2024 1

Yes, you might open an issue there (if there isn't one already).

from specification.

ardalis avatar ardalis commented on May 25, 2024

The first thing to check is if you can make the query work directly with your dBcontext. Apply your LINQ to dBContext.Players and see if you can get it to work. If not, the problem isn't related to this Specification package.

from specification.

fiseni avatar fiseni commented on May 25, 2024

Hi @mcchessers-sp,

Unfortunately, it has nothing to do with this library. We're passing the expression as it is. I suspect the MySQL provider doesn't know how to translate the filtered include which contains Take.

You may try to test without specifications, working directly with DbContext.

from specification.

fiseni avatar fiseni commented on May 25, 2024

Here I created a full test app using SqlServer provider. It works as expected. Try the same code with MySql provider.

using Ardalis.Specification;
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

await AppDbContext.SeedAsync();

using var dbContext = new AppDbContext();
var repo = new Repository<Customer>(dbContext);
var spec = new CustomerSpec();

var customer = await repo.FirstOrDefaultAsync(spec);

Console.WriteLine($"We fetched {customer!.Addresses.Count} addresses!");

public class CustomerSpec : Specification<Customer>
{
    public CustomerSpec()
    {
        Query.Include(x => x.Addresses.Take(2));
    }
}
public interface IRepository<T> : IRepositoryBase<T> where T : class
{
}
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class
{
    public Repository(AppDbContext dbContext) : base(dbContext)
    {
    }
}
public record Address
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
}
public record Customer
{
    public int Id { get; set; }
    public List<Address> Addresses { get; set; } = new();
}
public class AppDbContext : DbContext
{
    public DbSet<Customer> Customers => Set<Customer>();
    public DbSet<Address> Addresses => Set<Address>();
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=IncludeTest;Integrated Security=SSPI;ConnectRetryCount=0;";
        optionsBuilder.UseSqlServer(connectionString).LogTo(Console.WriteLine);
    }

    public static async Task SeedAsync()
    {
        using var dbContext = new AppDbContext();
        await dbContext.Database.EnsureDeletedAsync();
        await dbContext.Database.EnsureCreatedAsync();
        var customer = new Customer
        {
            Addresses = new List<Address>()
            {
                new Address(),
                new Address(),
                new Address()
            }
        };
        dbContext.Customers.AddRange(customer);
        await dbContext.SaveChangesAsync();
    }
}

from specification.

mcchessers-sp avatar mcchessers-sp commented on May 25, 2024

Thank you for the suggestions. I tried using DbContext outwith the specification:

DbContext.Players.Where(p => p.Id == (int)TestPlayers[0].Id).Include(p => p.ActiveDays.Take(1));

.. and got the same error, which would suggest that perhaps the driver (Pomelo) may be at fault?

from specification.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.