Search This Blog

Thursday, February 18, 2021

EF core with .net core 3.1

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")));
        }

Q36-Q40(EF)

Q36.  What is the difference between Include and Join in entity framework? Q37. What will be your approach in case you want to call a manual...