Close

Connecting Microsoft SQL Server Database in .NET Core Web Application

Using the following steps you can connect MSSQL Server with .Net Core applications.

In appsettings.json File (Using LocalDb) :

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "OnlineTicketingSystemCore": "Server=(LocalDb)\\MSSQLLocalDB;Database=OnlineTicketingSystemCoreDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

In appsettings.json File (Using Local) :

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "OnlineTicketingSystemCore": "Server=(Local);Database=OnlineTicketingSystemCoreDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

In DbContext :

public OnlineTicketingSystemCoreContext(DbContextOptions<OnlineTicketingSystemCoreContext> options)
            : base(options)
        { } 

In Startup.cs :

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<OnlineTicketingSystemCoreContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("OnlineTicketingSystemCore")));
// add other required code here
}

Leave a Reply

Your email address will not be published. Required fields are marked *