Wednesday, July 13, 2022

Nuget PMC Error while adding migration: The entity type 'Employee' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.

 This happens when you dont define primary key for an entity type. 


Define a primary key either explicitly or implicitly. 

Nuget PMC error: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

 This error means you have created the DbContext but not configured/added  it to the project using either DI in startup.cs or by using DbContext.OnConfiguring method. 


Add DbContext in starup.cs or configure it by overriding the virtual  DbContext.OnConfiguring  method.

============================================================

  1. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  2. {
  3. optionsBuilder.UseSqlServer("server=.;database=myDb;trusted_connection=true;");
  4. }

NOTE to use connectionString from appsetting.json. follow these steps:
1. In appsettings.json add ConnectionStrings section below allowedhosts and add a name-value pair for connection string.
2. Inject IConfiguration in DbContext constructor. You will need to add "using Microsoft.Extensions.Configuration" statement for this.
3. You can now get the conn string by using configuration.GetConnectionString("name");
    public class EmployeeContext : DbContext
    {
        
        private readonly IConfiguration configuration;
        public DbSet<Employee> Employees { get; set; }

        public EmployeeContext(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string str = configuration.GetConnectionString("conn");
            optionsBuilder.UseSqlServer(str);
        }
    }

===================================================================================


  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddDbContext<MyDbContext>(options => {
  4. options.UseSqlServer("server=.;database=myDb;trusted_connection=true;"));
  5. });
  6. }

OR
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<EmployeeContext>(options => {
            options.UseSqlServer(Configuration.GetConnectionString("conn")));
        }



NOTE that once you configure DbContext in ConfigureServices, you must wire it with DbContext by using a constructor that takes DbContextOptions as parameter and passes it to base contructor.


public class EmployeeContext : DbContext { public EmployeeContext(DbContextOptions<EmployeeContext> options) : base(options) { } DbSet<Employee> Employees { get; set; } }
  1. ===================================================================================

Monday, July 11, 2022

Git Pull Request

 1. Create new branch 

git checkout -b test 



2. Make changes and do add, commit and push. 

While pushing  changes, keep in mind you have to mention 

branch name as "test" 


git push origin test 


3. Now go to Azure and create a Pull Request. 


Select "test" as source branch and "main" as destination branch


Friday, July 1, 2022

Sql Server OFFSET and FETCH

 https://www.sqlservertutorial.net/sql-server-basics/sql-server-offset-fetch/



SQL Server OFFSET and FETCH

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...