Steps to use EF Core in project. (easy)
>> Go to Nuget packages and install "Microsoft.EntityFramework.SqlServer". This will install few more dependent packages with it.
>> Right click and create a folder named "Database"
>> Add a class and named it AppDbContext.cs (it could be any name)
>> Inherit the above class with "DbContext" class. "DbContext" class is available when we use 'using Microsoft.EntityFrameworkCore;' on top of a class file.
>> Create entities (tables) classes in model folder (or in same folder)
>> Create DB set property for above model class in AppDbContext class.
public class AppDBContext: DbContext{public AppDBContext(DbContextOptions<AppDBContext> options) : base(options){}public DbSet<Student> Students { get; set; }}
>> Now register the DBContext class in startup.ConfigureService method.
public void ConfigureServices(IServiceCollection services){services.AddDbContext<AppDBContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));}
Introduction:
-- EF core is a ORM (Object relational Mapper)
-- Only Code first approach is available. (DB first approach is very limited and should not be used)
-- Types of Classes 1) Domain Classes and 2) DB Context class
-- Domain class :: classes like employee, student, employeeDetails etc.
-- DBContext class -
-- EFcore supports both relatinoal and non relational DBs
No comments:
Post a Comment