ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)—修改数据库连接
2017-10-15 11:34
1196 查看
开发环境:vs2017 版本:15.3.5
项目环境:.net framework 4.6.1 模板asp.net core 2.0 Web应用程序(模型视图控制器)
身份验证:个人用户账号 存储应用内的用户帐户
因为本人并不涉及开发一些中、大规模的应用,所以习惯使用本地数据库,而不是数据库服务,为了方便管理,所以本人的所有项目都是离线数据库文件存储(.mdf)。
下面开始:
一、修改数据库连接。引自“张不水”兄的研究成果。
1、相对路径:
修改appsettings.json文件中的"ConnectionStrings"(第3行)
"DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true”
需注意的是:AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;
使用 ContentRootPath 是将文件放置在项目目录下而不是wwwroot目录下,这样更安全。
ContentRootPath 用于包含应用程序文件。
WebRootPath 用于包含Web服务性的内容文件。
实际使用区别如下:
ContentRoot: C:\MyApp\
WebRoot: C:\MyApp\wwwroot\
2、修改Startup.cs
自动生成的原始代码:
View Code
3、手动在项目中添加“App_data”文件夹,并复制粘贴一个标准的内容为空的.mdf文件。
为方便大家学习我这里为大家提供了示例数据库。
项目环境:.net framework 4.6.1 模板asp.net core 2.0 Web应用程序(模型视图控制器)
身份验证:个人用户账号 存储应用内的用户帐户
因为本人并不涉及开发一些中、大规模的应用,所以习惯使用本地数据库,而不是数据库服务,为了方便管理,所以本人的所有项目都是离线数据库文件存储(.mdf)。
下面开始:
一、修改数据库连接。引自“张不水”兄的研究成果。
1、相对路径:
修改appsettings.json文件中的"ConnectionStrings"(第3行)
"DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true”
需注意的是:AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;
使用 ContentRootPath 是将文件放置在项目目录下而不是wwwroot目录下,这样更安全。
ContentRootPath 用于包含应用程序文件。
WebRootPath 用于包含Web服务性的内容文件。
实际使用区别如下:
ContentRoot: C:\MyApp\
WebRoot: C:\MyApp\wwwroot\
2、修改Startup.cs
自动生成的原始代码:
public class Startup { public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; //新添加 _env = env; } public IConfiguration Configuration { get; } //新添加 public IHostingEnvironment _env { 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"))); //添加修改()声明变量conn并做相应处理 string conn = Configuration.GetConnectionString("DefaultConnection"); if (conn.Contains("%CONTENTROOTPATH%")) { conn = conn.Replace("%CONTENTROOTPATH%", _env.ContentRootPath); } //修改默认的连接服务为conn services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(conn)); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); // Add application services. services.AddTransient<IEmailSender, EmailSender>(); 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?}"); }); } }
View Code
3、手动在项目中添加“App_data”文件夹,并复制粘贴一个标准的内容为空的.mdf文件。
为方便大家学习我这里为大家提供了示例数据库。
相关文章推荐
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(四)—用户注册
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—启用用户管理
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(三)—用户账户及cookie配置
- 创建第一个ASP.NET CORE程序 使用DBFirst模式连接数据库
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- [ASP] asp.net 2.0 中使用web.config存储数据库连接字符串
- asp.net连接数据库 增加,修改,删除,查询代码
- ASP.NET中使用web.config配置数据库连接
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- ASP.NET中使用web.config配置数据库连接
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载)
- asp.net 2.0 中使用web.config存储数据库连接字符串
- Access (ASP.NET 开发知识)包括 (数据库连接知识 ) 和(socancode三层 使用的知识)
- ASP.NET - 如何:使用 SqlDataSource 控件连接到数据库
- ASP.NET对数据库的基本操作——插入,删除,修改(下)
- 树型节点与数据库的动态连接(asp.net 2.0 TreeView控件的使用)
- ASP.NET中使用web.config配置数据库连接
- 浪曦-ASP.NET-简易留言板------------------------2(数据库连接和使用)
- ASP.NET中使用web.config配置数据库连接