Recap Frameworkcore
Recap Frameworkcore
The CRUD operations / calling stored procedures are done with shorter amount of code
than ADO.NET.
Performance
So ADO.NET or its alternatives (such as Dapper) are recommended for larger & high-
traf c applications.
Strongly-Typed
fi
The columns as created as properties in model class.
So the Intellisense offers columns of the table as properties, while writing the code.
Plus, the developer need not convert data types of values; it's automatically done by
EFCore itself.
CodeFirst Approach
Manual changes to DB will be most probably lost because your code de nes the database.
Suitable for smaller applications or prototype-level applications only; but not for larger or
high data-intense applications.
DbFirst Approach
Migrations
Add-Migration MigrationName
Update-Database -Verbose
//Executes the migration; the database will be created or table schema gets updated as a
result.
Seed Data
in DbContext:
modelBuilder.Entity<ModelClass>().HasData(entityObject);
It adds initial data (initial rows) in tables, when the database is newly created.
fi
DbContext
DbSet
SELECT - SQL
LINQ Query:
_dbContext.DbSetName
Where(item => item.Property == value)
OrderBy(item => item.Property)
Select(item => item);
Add:
_dbContext.DbSetName.Add(entityObject);
//Adds the given model object (entity object) to the DbSet.
SaveChanges()
_dbContext.SaveChanges();
//Generates the SQL INSERT statement based on the model
object data and executes the same at database server.
Remove:
_dbContext.DbSetName.Remove(entityObject);
//Removes the specified model object (entity object) to the
DbSet.
SaveChanges()
_dbContext.SaveChanges();
//Generates the SQL DELETE statement based on the model
object data and executes the same at database server.
Update:
entityObject.Property = value;
//Updates the specified value in the specific property of the
model object (entity object) to the DbSet.
SaveChanges()
_dbContext.SaveChanges();
//Generates the SQL UPDATE statement based on the model
object data and executes the same at database server.
EF - Fluent API
DbContext class
modelBuilder.Entity<ModelClass>( ).HasIndex("column_name").IsUniqu
e();
modelBuilder.Entity<ModelClass>( ).HasCheckConstraint("constraint_
name", "condition");
}
}
DbContext class