How to connect your ASP.NET MVC 5 Project to an external SQL Server using Visual Studio Code?

Connecting your new ASP.NET MVC 5 Project using Visual Studio Code is very simple.  First and foremost you must create your project by following my previous post.

Then modify the Startup.cs class file.  There are two modifications, one is the constructor and the other one is the ConfigureServices method:

Constructor modification – Basically what we did was to modify the entry of  Data:DefaultConnection:ConnectionString.  You can, by the way, named the connection to any way you want or you can have multiple connections.

        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
                // For more details on using the user secret store see

            Configuration = builder.Build();
            Configuration["Data:DefaultConnection:ConnectionString"] =
                $@"Data Source=localhost;Initial Catalog=[DATABASENAME];Persist Security Info=True;User ID=[USER];Password=[PASSWORD]";
            //Configuration["Data:DefaultConnection:ConnectionString"] = $@"Data Source={appEnv.ApplicationBasePath}/rymage.db";

ConfigureService Method modification – Now for this modification you are to change your EntityFramework driver from Sqlite to SqlServer and use the default connection string you just modified at the constructor.

        public void ConfigureServices(IServiceCollection services)
            // Add framework services.
            // services.AddEntityFramework()
            //    .AddSqlite()
            //    .AddDbContext<ApplicationDbContext>(options =>
            //        options.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"]));

                    .AddDbContext<ApplicationDbContext>(options =>

            services.AddIdentity<ApplicationUser, IdentityRole>()


            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();

Now compile and run your application (dnx: Run Command->dnx web).  You should be able to see your ASP.NET membership in your database, like the image below:


That’s it!  And if you find this blog helpful, you can make the donation by clicking the Paypal button below.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s