0

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();
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • You've mixed up packages from different EF Core versions, all of which are discontinued for several years. Which EF Core version are you actually using? EF Core versions [follow the .NET Core support plan](https://learn.microsoft.com/en-us/ef/core/what-is-new/) so the oldest supported EF Core version is 6 – Panagiotis Kanavos Jun 15 '23 at 07:58
  • Aren't you getting package incompatibility or downgrade warnings already? Or did NuGet explicitly downgrade MySQL.Data.EntityFrameworkCore to a previous version that supports EF Core 2? – Panagiotis Kanavos Jun 15 '23 at 08:06
  • you are right it is ef core 2 and there is no incompatibility alert although they are deprecated but the project is old so haven't upgraded it yet – Awais Mukhtar Jun 16 '23 at 05:36
  • i have a diffrent layers and data layers is created as .net standard class library in which we play with mysql data... – Awais Mukhtar Jun 16 '23 at 05:51
  • You need to clean them up then. Conflicting EF Core versions means the layers don't work at all. `old so haven't upgraded it yet` it's way past time you did. EF Core 3 was released 3 years ago and was the first version that had parity with EF 6. Earlier versions used quirks like client-side evaluation to cover up missing features. – Panagiotis Kanavos Jun 16 '23 at 06:53

2 Answers2

0

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.

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
  • it says DbContextOptionsBuilder does not contains the definition of LogTo... – Awais Mukhtar Jun 16 '23 at 05:40
  • I am not sure why `DbContextOptionsBuilder does not contains the definition of LogTo...` in your project, But what I want to say is it will not get all the data in memory for filtering in your case. Maybe you can install `Microsoft.EntityFrameworkCore.Tools` and `Pomelo.EntityFrameworkCore.MySql`. – Xinran Shen Jun 16 '23 at 05:51
  • which packages should i remove if i installed your above ones? – Awais Mukhtar Jun 16 '23 at 05:57
  • sorry what's the version of your .net project? – Xinran Shen Jun 16 '23 at 06:00
  • its .netstandard 2.0 – Awais Mukhtar Jun 16 '23 at 06:42
  • 1
    @XinranShen the OP uses incompatible EF Core versions that are actually discontinued. There was no LogTo in EF Core 2. – Panagiotis Kanavos Jun 16 '23 at 06:45
  • @AwaisMukhtar add the runtime *and* EF Core version in the question itself. EF Core 5-6 don't support .NET Standard 2.0. The latest version that did was EF Core 3.1. Why are you targeting .NET Standard 2.0? Do you want to use the same library in .NET Framework and .NET Core applications? – Panagiotis Kanavos Jun 16 '23 at 06:50
  • actually the project is so old and haven't been updated to new versions...now i want to exactly verify what my question was related to contains how can i verify that – Awais Mukhtar Jun 16 '23 at 07:10
0

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.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • what should i do should i remove these two packages if i removed them which packages should i use to run ef core queries? – Awais Mukhtar Jun 16 '23 at 05:44
  • I explained which packages should be used. As for which EF Core version to use, it depends on which runtime you need to target. EF Core 5-6 target .NET Standard 2.1, so they only run on .NET Core 3+. The latest version that worked with .NET Standard 2.0 is EF Core 3.1. Unless you want to use your library in .NET Framework though, there's no reason to target .NET Standard 2.x at all – Panagiotis Kanavos Jun 16 '23 at 06:52