{"id":28895,"date":"2019-04-03T00:00:00","date_gmt":"2019-04-02T22:00:00","guid":{"rendered":"https:\/\/blexin.com\/mai-fidarsi-di-entity-framework\/"},"modified":"2021-05-20T19:20:32","modified_gmt":"2021-05-20T17:20:32","slug":"never-trust-in-entity-framework","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/","title":{"rendered":"Never trust in Entity Framework!"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-attachment-id=\"28877\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/ad91fe80-751b-4d34-a054-0444bfc8545a-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&amp;ssl=1\" data-orig-size=\"1024,608\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ad91fe80-751b-4d34-a054-0444bfc8545a\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?resize=1024%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-28877\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Well, the title is surely overstated, and we can assert the same for all the ORM. As per everything, we need to know how they are made, in order to use them with awareness. Anyway, it was the first thing that comes on my mind as, during a consulting meeting with a new client, we realize that, despite our concentration, we were looking at the wrong criminal (in that case, Azure). Let\u2019s proceed in an orderly fashion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Following the event in Napoli Hello Azure DevOps (<a href=\"https:\/\/www.blexin.com\/it-IT\/Event\/Hello-Azure-DevOps-5\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.blexin.com\/it-IT\/Event\/Hello-Azure-DevOps-5<\/a>), a company did contact us to ask for our cooperation in the migration of their application. It was a Window Form solution, that has to migrate to more up-to-date technologies. Its requirements were: to be cross -platform, to have a as-a-service solution and performances in agreement with their standards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We were happy for this request and I went by them to plan some consulting days to prepare a prototype to test the migration feasibility. We focus particularly ourselves on the aspect, which was much critical for them: the check-in for a healthcare facility. The scenario is a public office, where the employee must manage a queue of people waiting the sooner as possible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From a technological point of view, we choose to test the Asp.Net Core for the backend, Angular for the frontend, leaving SQL Server as database, but with the possibility to change to PostgreSQL. Everything can be hosted both on Azure and on premise, and both on Windows and on Linux.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We start from the backend with one of the Asp.Net Core standard templates, which are provided from the Command Line Interface (CLI). We add the authentication with Asp.Net Identity, which is needed to profile users, and provide us also a DbContext Entity Framework Core to use to manage our entities. If you already installed the .NET Core CLI (<a href=\"https:\/\/dotnet.microsoft.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/dotnet.microsoft.com\/<\/a>) , you just need to create a folder and run the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\ndotnet new mvc --auth Individual\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">For simplicity&#8217;s sake, we will focus on a single aspect of the prototype: the research of patients\u2019 personal data. The patient usually arrives to the check-in and provides his\/her name and last name. It is a quite simple class to create, as the example below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Paziente\n{\n\u00a0\u00a0\u00a0\u00a0public int Id { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0&#x5B;Required]\n\u00a0\u00a0\u00a0\u00a0&#x5B;StringLength(50)]\n\u00a0\u00a0\u00a0\u00a0public string Nome { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0&#x5B;Required]\n\u00a0\u00a0\u00a0\u00a0&#x5B;StringLength(50)]\n\u00a0\u00a0\u00a0\u00a0public string Cognome { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public DateTime DataNascita { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0&#x5B;Required]\n\u00a0\u00a0\u00a0\u00a0&#x5B;StringLength(16)]\n\u00a0\u00a0\u00a0\u00a0public string CodiceFiscale { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0&#x5B;Required]\n\u00a0\u00a0\u00a0\u00a0&#x5B;StringLength(200)]\n\u00a0\u00a0\u00a0\u00a0public string Indirizzo { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0&#x5B;Required]\n\u00a0\u00a0\u00a0\u00a0&#x5B;StringLength(6)]\n\u00a0\u00a0\u00a0\u00a0public string CAP { get; set; }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Add then the class to the ApplicationDbContext:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class ApplicationDbContext : IdentityDbContext\n{\n\u00a0\u00a0\u00a0\u00a0public DbSet&lt;Paziente&gt; Pazienti { get; set; }\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0: base(options)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">To simulate the worst situation one could face, we create e WebApp on Azure on a Service Plan F1 (the free one) and a SqlAzure Basic instance for \u20ac 4,21\/month (there\u2019s also a free instance for SqlAzure). You can have same results and much more with a free Azure trial. Once services have been created, you can take the connection string to database, but please remember to add your IP to the firewall, if you want to link to the server to make local tests:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"558\" data-attachment-id=\"28881\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/figura01-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura01.png?fit=1524%2C830&amp;ssl=1\" data-orig-size=\"1524,830\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"figura01\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura01.png?fit=1024%2C558&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura01.png?resize=1024%2C558&#038;ssl=1\" alt=\"\" class=\"wp-image-28881\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura01-980x534.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura01-480x261.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the connection string (note that username and password are not present for security reasons, but you have chosen them during the creation of the service):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"340\" data-attachment-id=\"28883\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/figura02-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura02.png?fit=1524%2C506&amp;ssl=1\" data-orig-size=\"1524,506\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"figura02\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura02.png?fit=1024%2C340&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura02.png?resize=1024%2C340&#038;ssl=1\" alt=\"\" class=\"wp-image-28883\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura02-980x325.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura02-480x159.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">and paste it in the file appsettings.json:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n{\n\u00a0\u00a0&quot;ConnectionStrings&quot;: {\n\u00a0\u00a0\u00a0\u00a0&quot;DefaultConnection&quot;: &quot;Server=tcp:efcoretest.database.windows.net,1433;Initial Catalog=efcoretest;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;\n\u00a0\u00a0},\n\u00a0\u00a0&quot;Logging&quot;: {\n\u00a0\u00a0\u00a0\u00a0&quot;LogLevel&quot;: {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&quot;Default&quot;: &quot;Warning&quot;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0},\n\u00a0\u00a0&quot;AllowedHosts&quot;: &quot;*&quot;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">we just need to create the Entity Framework Migration and apply it to the database:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\ndotnet ef migrations add Pazienti \ndotnet ef database update\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We have obviously uploaded some data in the test, about 25000 lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using a Mac, please note: in the Startup.cs file the default provider of the template for Entity Framework is set on SqlLite and you have to change it to SqlServer:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nservices.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;\n\u00a0\u00a0\u00a0\u00a0options.UseSqlServer(\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you need to test the connection to the database from a Mac, you can use Azure Data Studio (<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.microsoft.com\/it-it\/sql\/azure-data-studio\/download?view=sql-server-2017\" target=\"_blank\">https:\/\/docs.microsoft.com\/it-it\/sql\/azure-data-studio\/download?view=sql-server-2017<\/a>): it\u2019s free and very similar to VSCode!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"758\" data-attachment-id=\"28887\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/figura03-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03.png?fit=1099%2C814&amp;ssl=1\" data-orig-size=\"1099,814\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"figura03\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03.png?fit=1024%2C758&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03.png?resize=1024%2C758&#038;ssl=1\" alt=\"\" class=\"wp-image-28887\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03-1024x758.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03-980x726.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura03-480x356.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At this stage, we only have to add a controller and write our query:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class PazientiController : Controller\n{\n\u00a0\u00a0\u00a0\u00a0private readonly ApplicationDbContext ctx;\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public PazientiController(ApplicationDbContext ctx)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.ctx = ctx;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public IActionResult RicercaPazienti(string nome, string cognome)\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var results = this.ctx.Pazienti\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.Where(x =&gt; x.Nome.StartsWith(nome) &amp;&amp; x.Cognome.StartsWith(cognome))\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.Take(50)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.ToList();\n\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return Ok(results);\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">It works, good. Unfortunately, in Windows Form application the selection of personal data gests users used to digit name and last name, with an update of result list every time one of these parameters changes. The effect is that this API will be recalled at any digit in one of these two fields, and we replicate this behavior in Angular.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The obvious result is that we don\u2019t obtain the same feedback of the original application. Someone may asserts that is normal to lose something between the query and the serialization of the result in JSON: you are passing from a local environment to a Web one, from SQL Server in local network to Sql Azure in a Cloud, from an application on client, to an interaction Angular-Asp.Net Core-SqlServer. The purpose of the prototype was to test these chances indeed, then, together with Roberto, the client\u2019s reference person, we start to make some tests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We began from simplest activities, such as ascending the database, passing to a stronger and more expensive instance. The situation improves itself, of course, but not enough. Roberto adds two indices on name and last name: the situation improves again, but not enough. We try to change the UX, then press the Enter key to start the research: a great result, but we were still not satisfied at all. There was something wrong. Roberto, that works for year with data and SQL Server suddenly said to me: \u201cCan you please let me have a look at the query that generates Entity Framework?\u201d. The query is easily visible in console logs:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"202\" data-attachment-id=\"28889\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/figura04-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04.png?fit=1085%2C214&amp;ssl=1\" data-orig-size=\"1085,214\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"figura04\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04.png?fit=1024%2C202&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04.png?resize=1024%2C202&#038;ssl=1\" alt=\"\" class=\"wp-image-28889\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04-1024x202.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04-980x193.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura04-480x95.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The new question then was: why did it make that type of WHERE? Note that Entry Framework is an ORM and the Core version has been completely rewrote to support some new scenarios, as databases in memory (very useful for automatic tests) and the management of non-relational databases (well, also ask myself, why should I use an ORM with a non-relational database). Furthermore, LINQ can be used also in scenarios with no database, and this is one of its point of strength.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We search on the web too, and we find out that this is a known issue, and It is generally linked to the use of the StartWith() and the Contains() on the strings. Frameworks developers lucky provide us specific functions to allow us to be more direct on LINQ, when we know we will work on a database:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic IActionResult RicercaPazienti(string nome, string cognome)\n{\n\u00a0\u00a0\u00a0\u00a0var results = this.ctx.Pazienti\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.Where(x =&gt; EF.Functions.Like(x.Nome, nome + &quot;%&quot;) &amp;&amp; EF.Functions.Like(x.Cognome, cognome + &quot;%&quot;))\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.Take(50)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.ToList();\n\u00a0\n\u00a0\u00a0\u00a0\u00a0return Ok(results);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The resulting query is:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"156\" data-attachment-id=\"28892\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/attachment\/figura05-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05.png?fit=1089%2C166&amp;ssl=1\" data-orig-size=\"1089,166\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"figura05\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05.png?fit=1024%2C156&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05.png?resize=1024%2C156&#038;ssl=1\" alt=\"\" class=\"wp-image-28892\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05-1024x156.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05-980x149.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/figura05-480x73.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The result is amazing: the speed detected from the application is exactly the one we need, even coming back to the base version of Azure SQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The bottom line is: when you are working with an ORM, Entity Framework or any other, don\u2019t lose the sight of the generated queries. Disable the Lazy Load and manually set the fetch plan with the Include() when needed, and try on, try on, try on!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Coding!<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use ORM with awareness and attention<\/p>\n","protected":false},"author":196716248,"featured_media":28877,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","inline_featured_image":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[688637524],"tags":[688637416,688637381],"class_list":["post-28895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-asp-net-core-en","tag-azure-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Never trust in Entity Framework! - Blexin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Never trust in Entity Framework! - Blexin\" \/>\n<meta property=\"og:description\" content=\"Learn how to use ORM with awareness and attention\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-02T22:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-20T17:20:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Michele Aponte\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michele Aponte\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/\"},\"author\":{\"name\":\"Michele Aponte\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/cdc5540c3b6edcacd8d760669e797005\"},\"headline\":\"Never trust in Entity Framework!\",\"datePublished\":\"2019-04-02T22:00:00+00:00\",\"dateModified\":\"2021-05-20T17:20:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/\"},\"wordCount\":1107,\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1\",\"keywords\":[\"Asp.net core\",\"Azure\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/\",\"name\":\"Never trust in Entity Framework! - Blexin\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1\",\"datePublished\":\"2019-04-02T22:00:00+00:00\",\"dateModified\":\"2021-05-20T17:20:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/cdc5540c3b6edcacd8d760669e797005\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/blexin.com\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1\",\"width\":1024,\"height\":608},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/blog-en\\\/never-trust-in-entity-framework\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blexin.com\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Never trust in Entity Framework!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blexin.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blexin.com\\\/en\\\/#\\\/schema\\\/person\\\/cdc5540c3b6edcacd8d760669e797005\",\"name\":\"Michele Aponte\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g\",\"caption\":\"Michele Aponte\"},\"url\":\"https:\\\/\\\/blexin.com\\\/en\\\/author\\\/michele-aponteblexin-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Never trust in Entity Framework! - Blexin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/","og_locale":"en_US","og_type":"article","og_title":"Never trust in Entity Framework! - Blexin","og_description":"Learn how to use ORM with awareness and attention","og_url":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/","og_site_name":"Blexin","article_published_time":"2019-04-02T22:00:00+00:00","article_modified_time":"2021-05-20T17:20:32+00:00","og_image":[{"width":1024,"height":608,"url":"https:\/\/i2.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","type":"image\/png"}],"author":"Michele Aponte","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Michele Aponte","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/"},"author":{"name":"Michele Aponte","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/cdc5540c3b6edcacd8d760669e797005"},"headline":"Never trust in Entity Framework!","datePublished":"2019-04-02T22:00:00+00:00","dateModified":"2021-05-20T17:20:32+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/"},"wordCount":1107,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","keywords":["Asp.net core","Azure"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/","url":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/","name":"Never trust in Entity Framework! - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","datePublished":"2019-04-02T22:00:00+00:00","dateModified":"2021-05-20T17:20:32+00:00","author":{"@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/cdc5540c3b6edcacd8d760669e797005"},"breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","width":1024,"height":608},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/never-trust-in-entity-framework\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"Never trust in Entity Framework!"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/cdc5540c3b6edcacd8d760669e797005","name":"Michele Aponte","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/32138aff568f2063b34d27a23cef27e09f3159bfcadea5ea05599c499cf4342f?s=96&d=identicon&r=g","caption":"Michele Aponte"},"url":"https:\/\/blexin.com\/en\/author\/michele-aponteblexin-com\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/ad91fe80-751b-4d34-a054-0444bfc8545a.png?fit=1024%2C608&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-7w3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/users\/196716248"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=28895"}],"version-history":[{"count":2,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28895\/revisions"}],"predecessor-version":[{"id":31999,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28895\/revisions\/31999"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/28877"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=28895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=28895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=28895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}