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.json")
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
                builder.AddUserSecrets();
            }

            builder.AddEnvironmentVariables();
            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"]));

            services.AddEntityFramework()
                    .AddSqlServer()
                    .AddDbContext<ApplicationDbContext>(options =>
                         options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // 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:

b1

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


		
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s