What is ORM?
Its stands for Object Relational Mapping. Its one of the way to interact with DBMS (Oracle, MS Sql, MySQL,...) from a server side technology (.NET, JAVA, NodeJS,...)
What is EntityFramework?
Its the implementation of the ORM concept by Microsoft
How EF works?
Map table name to class name
Map table fields to class attribute members
Work on the classes and attributes instead directly on underlying database which is taken by the layer EF framwork provided by Microsoft
What are all the possible ways one can implement EF in his/her project?
It can be implemented
DB first (or) Code First
DB first : DB object will be created then above said mapping done
Code first: All DB objects would be defined in terms of classes and attributes and through this create the DB
Can you show me a practical example of implementing CodeFirst approach?
It is handled using "Migrations" concepts in MS entity framework
Migrations => its a simple files in which EF maintains
1.entity details (ie., table details) and relevant DB scripts in EF using MODEL builder
modelBuilder.Entity("productservice.model.Category", b =>
{
b.Property<int>("CategoryID")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Description");
b.Property<string>("Name");
b.HasKey("CategoryID");
b.ToTable("Category");
b.HasData(
new { CategoryID = 1, Description = "Electronics itmes", Name = "Electronics" },
new { CategoryID = 2, Description = "dresses", Name = "Clothes" },
new { CategoryID = 3, Description = "Grocery items", Name = "Grocery" },
new { CategoryID = 4, Description = "liquid items", Name = "Liquid" }
);
});
2.DB scripts (ie., DDL & DML) in the C# object format (not in T-SQL format) using MIGRATION builder
EX:
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "Products",
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.InsertData(
table: "Category",
columns: new[] { "CategoryID", "Description", "Name" },
values: new object[] { 4, "liquid items", "Liquid" });
}
How to enable the migration in CodeFirst approach in Visual Studio?
> In Visual Studio, Tools => Nuget Package Manager => Package Manager Console
> Enable-Migrations (just once for the lifetime of your project)
How to create and run a migration
Step 1 : Create a migration (form a model and migration builder) for all avialable DbContext classes in the project (ex: ProductContext : DbContext {....} )
> open package manager console (Tools => Nuget Package Manager => Package Manager Console)
> add-migration <<name the migration which is equivalent to label in source repository>>
> it will create below 3 files and grouped using the given migration name under folder "Migrations"
1) Up (rollout)=> migration builder
2) Down (rollback) => migration builder
3) BuildTargetModel => model builder
> Example, add-migration InitialCreate (it will create 2019011XXXXX_InitialCreate.cs under above 3 files created, but the migration name is "InitialCreate" alone)
Step 2 : Apply the migration
> update-database (it will run the create Step 1 scripts against the DB and made changes)
If any new changes made in the entity then again create a new migration "add-migration newmigrationname" and run again the "update-database"
How to rollback a migration
update-database -migration <<migrationname>>
ex: update-database -migration InitialCreate (it will roll back the "newchanges1" to "InitialCreate" changes )
How to see the migration versions maintained by EF?
It will create a table called __EFMigrationsHistory with 2 columns (MigrationId, ProductVersion)
You can also manually form a script using below command to create this migration history table and run in the database
> Script-Migration
To generate all scripts below command can used to generate
> Script-Migration -from 0