Skip to main content

EF CORE 6

 Enumerating through the results


3. Everytime we enumerate thorugh the DBset inside a foreach loop, the connection to the database remains open. So doing a lot of work inside the loop will result in the database connection to stay open. So, it's better to run a ToList() and get the 
results in memory before we start iterating through the results.

SQL Injection

If we hardcode the value for a parameter in the LINQ query then it will be hardcoded in the query as well, so try to use variables. Variables are converted to parameters and hence prevent SQL injection.  

Partial Filtering

We can have more control on the LIKE statement when using the Ef.Functions.Like method
 var authorsNameEndWith = await context.Authors.Where(a => EF.Functions.Like(a.FirstName, "%a")).ToListAsync();

Find 

Order By

If we have multiple order by statements, then LINQ will ignore all but the last one. So, use the thenBy operator to add more orderby statements.

Triggering the query execution

No Tracking

Not all scenarios need the entity to be tracked. For Ex: When we try to display all the authors in the database. We just need to query and return the data. Change tracking is expensive. 
We can configure the entire Dbcontext to be non-tracking and we can maintain 2 contexts separately in the solution and use them accordingly.  

DBContext and Tracking

DbContext represents a session with the database.
Everytme, we interact with the database, it goes the the cycle shown in the image above. Finally when the query is executed it also needs to reset the state information to unchanged. 

SaveChanges with an update, will return the rowcount. This count will be equal to the number of rows updated. It doesnt do the same with Add, because Add will always insert a single object. 





















































































































Comments

Popular posts from this blog

App Role assignment to service principal --

 Using Ms Graph Rest API's Permissions One of the following permissions is required to call this API. To learn more, including how to choose permissions, see  Permissions . Permission type Permissions (from least to most privileged) Delegated (work or school account) AppRoleAssignment.ReadWrite.All and Application.Read.All, AppRoleAssignment.ReadWrite.All and Directory.Read.All, Application.ReadWrite.All, Directory.ReadWrite.All Delegated (personal Microsoft account) Not supported. Application AppRoleAssignment.ReadWrite.All and Application.Read.All, AppRoleAssignment.ReadWrite.All and Directory.Read.All, Application.ReadWrite.All, Directory.ReadWrite.All Create 2 app registrations. App role owner will contain the app role that will be assigned to a service principal. The  reader role in approleowner will be added to the approlesubscriber Setup postman to use the Oauth auth flow to get a token for MS Graph. ClientId:   Application (client) ID for approlesubscrib...

ASp.net core 3.1 identity

It is just an extension to cookie authentication. We get a UI, Tables, helper classes, two factor authentication etc. Even EF and its database constructs. So instead of writing code for all of this we can just use these in built features. Extending Default Identity Classes Add a class that inherits from    public class AppUser : IdentityUser     {         public string Behavior { get; set; }     } Also change the user type in login partial.cs under shared folder Then add migrations and update db using migrations. We can customize further.  services.AddDefaultIdentity<AppUser>(options =>              {                 options.SignIn.RequireConfirmedAccount = true;                 options.Password.RequireDigit = false;           ...

Get user groups

 string[] scopes = new string[] { "https://graph.microsoft.com/.default" };             string clientId = "";             string tenantId = "";             string secret = "";                        var options = new TokenCredentialOptions             {                 AuthorityHost = AzureAuthorityHosts.AzurePublicCloud             };             // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential             try             {                 var clientSecretCredential = new ClientSecretCredential(                        ...