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
About

Software developer living in the Philippines.

Posted in ASP.NET

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

Author

Harold Glenn P. Minerva
Software Developer / Tech Enthusiast
Living in the Philippines

View Harold Glenn Minerva's profile on LinkedIn

Instagram

Software Engineer - Seasonal and Range Trading Software. Magenta Trader is a powerful stock market visualization software that increases your probability of trading success.

Software Architect and Founder - Easyfis.com is a multi-tenant cloud-based Software-as-a-Service (SaaS) business app that caters to micro, small and medium trading businesses.

CTO and Co-Founder - We give your company the leverage by providing innovative software solutions products such as Point-of-Sales (POS), Financial Information System (FMIS), Payroll and DTR (HRIS), and many more.

%d bloggers like this: