skip to Main Content

Nuget lib am using MySQL.Data.EntityFrameworkCore 8 and Microsoft.EntityFrameworkCore.Relational 2.2.6.
I am using the mysql database with entity framework core and am not sure contains method in linq is directly supported by entityframwork core or not I have huge records and alot of traffic on my site am worrying it will load records in memory and then do contains search

return await dbContext.Table
    .Where(x => x.Url.Contains(somestring))
    .Select(x => x.Field)
    .ToListAsync();

2

Answers


  1. Of course it can support Contains(), Here is a simple query:

    _dbcontext.TestModels.Where(x => x.Name.Contains("ac")).ToList();
    

    Now I will log the sql which ef core generate in dbcontext

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                base.OnConfiguring(optionsBuilder);
                optionsBuilder.LogTo(x =>
                {
                    Console.WriteLine(x);
                });
            }
    

    Check the log file

    enter image description here

    You can find it generate sql correctly.
    But if I put the .Tolist() method in front of the query condition, The query like:

     _dbcontext.TestModels.ToList().Where(x => x.Name.Contains("ac")); 
    

    check the log file

    enter image description here

    It will not add any query condition in sql, That’s what you’re worried about getting all the data in memory for filtering.

    You can refer to this link to learn the different between IQueryable<T> and IEnumerable<T>, It will make you know more about this question.

    Login or Signup to reply.
  2. The packages and versions are mixed up and by now, discontinued. The reason the query doesn’t work is you’re trying to use an EF Core 3 provider in EF Core 2.

    The solution is to remove Oracle’s providers completely and add the Pomelo.EntityFrameworkCore.MySql version that matches the EF Core version you use. Keep in mind that EF Core versions follow the .NET Core support plan. Right now, the oldest supported .NET Core version is .NET 6, so the oldest supported EF Core version is EF Core 6.

    Details

    Oracle’s MySql.Data.EntityFrameworkCore was discontinued 3 years ago because it had too many problems and was badly maintained. It was replaced by MySql.EntityFrameworkCore but even that has so many issues that people just don’t use Oracle’s providers.

    Second, that package’s Dependencies page shows it required at least Microsoft.EntityFrameworkCore.Relational v3.1.1. That’s part of EF Core 3.1, which is so old it got discontinued as well. Microsoft.EntityFrameworkCore.Relational v2.2.6 was part of EF Core 2.2, discontinued in December 23, 2019

    Finally, Oracle’s providers have enough problems that people overwhelmingly prefer using the truly open source Pomelo.EntityFrameworkCore.MySql and its MySqlConnector ADO.NET provider.

    The numbers aren’t even close: 42M downloads for Pomelo, 8M for Oracle’s providers combined. If you compare just with the "new" package (released 2020), its’ 42M vs 3M.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search