EFCore

 EFCore:













If we apply multiple OrderBy in the linq queries, it will take the last one and skip the rest of the things. If you want sort the result based on multiple condition then we should use OrderBy with ThenBy for the ascending order. OrderByDescending with ThenByDescending for the descending order.










When we use FirstOrDefault it uses select top 1




When we use LastOrDefault, we should use Orderby otherwise it will throw exception at run time. No compile time error


Solution code for it

When we use first/firstordefault/last/lastordefault/single/singleordefault, we should use orderby or orderbydescending before the firstordefault method



Tracking is always expensive, avoid it where it's not required 
1.First option to make the particular query as no tracking.
2. second options to make all the query as no tracking, in case if you want to particular query as tracking use as tracking method  "AsTracking()"











When we are writing generic method that time we use context.Add method that time dbcontext will identity which type it's added and keep the track of it.

Update without using update method:



When we update multiple rows at time and save it, it will create transaction and execute the statements. if single row it will not create transaction.



Update objects using Update method


When we use update method, it will update all the field irrespective of any field it's changed. For example if  you changed one field value also it will update other fields also.





When we want to add multiple rows, we can use range method and it's creating merge sql statement, you can see that in the below images


Execute delete method will not have any track of the object, it will execute immediately and it will not check either value present or not to delete it.
Execute update method





Above privateassets and includeassets are used  for, this package will  be  only for this project and it will not used in the dependency project and it will be used only for the build time only.








Reverse Engineering done using scaffolding:




Relationships:
One to Many:

  Still Ef core identity the AuthorId as foreign key relationship with the author class.

  There is no relationship between author and book class actually.


If you want to relationship for the non navigation property then we have to make changes in the OnModelCreating


Better to keep foreign key property id in the class like below. it's best practices actually. it will help when we are seeding the data as well.




If you want to save the data without base/depent class (Author), we can do it three ways mentioned below

Logging:





Comments

Popular posts from this blog

Azure Service Bus Azure CLI Commands

Securing a Web API using Azure AD and Consuming it with Swagger - Step by Step Guide

Service Bus Queues and Topics