using DotNetCore20.Entity; using Microsoft.EntityFrameworkCore; namespace DotNetCore20.DAL.DbContext { public class DotNetCoreDbContext : Microsoft.EntityFrameworkCore.DbContext { public DotNetCoreDbContext(DbContextOptions<DotNetCoreDbContext> options) : base(options) { } public DbSet<UserExtend> UserExtend { get; set; } } }
在此基本的实体映射相关的代码都完毕,现在还有一步,就是数据库连接字符串的配置
首先打开appsettings.json文件,在ConnectionStrings节点下增加以下
复制代码 代码如下:
"DotNetCoreConnection": "Server=(localdb)\\mssqllocaldb;Database=DotNetCoreDb;Trusted_Connection=True;MultipleActiveResultSets=true"
增加后如下:
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DotNetCoreDefaultDb;Trusted_Connection=True;MultipleActiveResultSets=true", "DotNetCoreConnection": "Server=(localdb)\\mssqllocaldb;Database=DotNetCoreDb;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } } }
再打开web网站下的Startup文件,在ConfigureServices方法中添加一下行:
//自定义数据库连接字符串 services.AddDbContext<DotNetCoreDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DotNetCoreConnection")));
增加后如下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using DotNetCore20.Web.Data; using DotNetCore20.Web.Models; using DotNetCore20.Web.Services; using DotNetCore20.DAL.DbContext; namespace DotNetCore20.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //自定义数据库连接字符串 services.AddDbContext<DotNetCoreDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DotNetCoreConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
运行程序,点击登陆(只要访问数据库的操作都可),出现错误页面:
点击应用迁移,即自动迁移数据库.
由于两个数据库,只会自动迁移关于用户的表AspNetUsers,
所以还得VS中程序包管理器中下命令迁移.
Add-Migration firstMigration -Context DotNetCoreDbContext
以上命令执行后再执行以下命令:
Update-Database -ContextDotNetCoreDbContext;
然后查看数据库会发现多出两个数据库,
以DotNetCoreDefaultDb生成的为例,会生成如下表:
其中AspNetUsers就中会有之前添加的WeChatId字段
然后再次运行程序:
这样一个完整的Asp.NetCore2.0网站就初步运行起来了