Tuesday, October 11, 2016

Fully Working Code Sample: ASP.NET MVC Core 1.1, Entity Framework Core, Database First (2)

1) Open KCMSContext.cs file under Entities folder,

replace


1
2
3
4
5
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
        }



with


1
2
3
        public KCMSContext(DbContextOptions<KCMSContext> options)
            : base(options)
        { }


2) Open startup.cs, in the method: public void ConfigureServices(IServiceCollection services)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
        public void ConfigureServices(IServiceCollection services)
        {
            var connection = @"Data Source=CTSCKVCMSSQLTst;Initial Catalog=KCMS2_Migration;Integrated Security=True;Persist Security Info=True;MultipleActiveResultSets=True;";
            services.AddDbContext<KCMSContext>(options => options.UseSqlServer(connection));

            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

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

            services.AddMvc();

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




No comments:

Blog Archive