/ORM

A set of simple applications to learn ORM-EF

Primary LanguageC#

ORM - Entity Framework (Cheat Sheet)

This repo includes a set of simple applications to learn ORM-EF

Installation

Global Installation

Make sure to have entity framework installed. Write the following command in CMD/Powershell

dotnet ef [or] dotnet ef --version

If not installed, install it by running (in c/windows/system32)

dotnet tool install --global dotnet-ef

You can also check installed SDK versions

dotnet --list-sdks

Code, packages Installment and configs

1.Use the following package managers to configure and use EF in code

• Microsoft.EntityFrameworkCore.Tools (6.0.13)
• Microsoft.EntityFrameworkCore.Design (6.0.13)
• Microsoft.EntityFrameworkCore.SqlServer (6.0.13)
• Microsoft.EntityFrameworkCore.Proxies (6.0.13) 
	--> Since we are using EntityFrameworkCore and not just EntityFramework(6-> the most typical)
  1. Add a ConnectionString in the Appsettings
  2. Create a contextClass, deriving from the ef DbContextClass
  3. Configure EF context in the configureServices of StartUP(or directly in Program.cs if you're using .Net 6 or higher) --> This is in case of an Core Web App. If you're using Console APP you have to create a factory class.

Example of the last 3 steps

  1. Add a ConnectionString in the Appsettings like so:

       "ConnectionStrings": {
     	"AppDb": "Server=.;Database=School;Trusted_Connection=true;TrustServerCertificate=True;Integrated Security=true"
       },
    
  2. public class SchoolContext : DbContext

  3. Configure EF context directly in Program.cs like so:

    builder.Services.AddDbContext(x => x.UseLazyLoadingProxies() .UseSqlServer(connectionString) .UseLoggerFactory(LoggerFactory.Create(builder=>builder.AddConsole())));

Migration Commands

dotnet ef migrations add InitialCreate
dotnet ef database update

In case we want to remove a migration two commands can be considered, based on whether the migration is already applied against a database or not

dotnet ef database update <previous-migration-name>
dotnet ef migrations remove (removes the last migration)

Models and Relations

To turn models into DB tables, rules must be followed.

• EF has it's own internal conventions on which it works to translate 
  models and props into tables and columns
• DataAnnotation on properties are taken into account as well
• FluentAPI is another way of configuring Models and Props

Each approach has pros and cons.

Ex. DataAnnotations are easy to understand and use, but are tighly coupled to the model class.

FluentAPI has the highest priority.Then it falls back to DataAnnotations and then to NamingConventions The way you design models and define props will affect the way SQL queries are generated. Conventions are overriden by DataAnnotations, which are overriden by FluentAPI

LazyLoading vs Eager Loading vs Explicit Loading

Eager Loading is pretty much self explanatory. Load data all at once(in the same SQL Query). This is achieved by using .Include statement.

Lazy Loading is loading data when first requested. A new SQL query is generated for them. Lazy Loading in EF CORE(which is different from EF6) is disabled by default. This means that if we try lazy-load we will get a null/empty/default navigation property value.

Even when disabled we can lazy-load navigation properties by using Explicit Loading. This is achieved by the following syntax

_dbContext.Entry(std).Collection(s => s.Projects).Load();
 var project = std.Projects.FirstOrDefault();

To enable Lazy-Loading

builder.Services.AddDbContext<SchoolContext>(x =>
			x.UseLazyLoadingProxies()

Working with an existing Database

Scaffold-DbContext "Server=.;Database=School;Trusted_Connection=1" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context SchoolContext -Force

Nepermjet kesaj komande, te runuar ne VS PackageManagerConsole, gjenerohet databaza me modelet dhe context-in qe percaktojme. Ne konfigurojme vetem CNN, driverin(SqlServer package ne kete rast) dhe path ku do krijohen modelet. Me keto konfigurime, do shohim qe modelet e gjeneruara do jene konfiguruar duke perdorur FluentAPI.

Nese duam qe modelet ti gjenerojme duke perdorur data annotations, duhet te perdorim edhe nje flag tjeter.(0DataAnnotations) Use -force(f) flag to overwrite existing models.

Scaffold-DbContext "Server=.;Database=School;Trusted_Connection=1" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context SchoolContext -DataAnnotations -Force

Working with other databases.

To work with databases other than SQL Server you just have to install EntityFrameworkCore.[DbName] package. No need to install Tools or Design Packages. After that, you just have to use the package AddDatabase method in the ContextConfiguration, just as we did for the SQL, and stick to that DB CNN format. In some cases such as CosmoDB(Azure-json document-noSQL database), int ID PK and autoincrement does not make sense and we have to convert that into a string with a generated GUID default value.

Git commands

Adding a .gitIgnore file

open git bash in any of the repo projects that you want to add the .gitIgnore file and write touch .gitIgnore A .gitIgnore file is generated. There we can add whatever we want to ignore. We can use a tool like https://www.toptal.com/developers/gitignore to create useful .gitignore files for our proj. Just write keywords like visualStudio, netCore, Windows ecc and c/p the generated content. In case we haven't checked out the files we want to ignore, these are all the required steps to add a gitIgnore. In the files are already checked out, we need to remove them from the cache first. And later on check them out again, with the .gitIgnore file. To do this, simply open gitbash in the proj main directory and write cmds like $ git rm -r --cached bin or $ git rm -r --cached obj or $ git rm -r --cached .vs