.net 和 core2.0 数据库连接字符串
Asp.net Core 数据库离线文件的连接(引自“张不水”兄的研究成果。)
一、绝对路径:
"DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;AttachDbFilename=C:\\Users\\Administrator\\Documents\\Visual Studio 2017\\Projects\\WebApplication1\\WebApplication1\\App_Data\\aspnet123.mdf;Integrated Security=True;Connect Timeout=30"
二、相对路径:
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)
{
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.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?}");
});
}
}
修改后代码
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?}");
});
}
}
3、我们需要手动在项目中添加“App_data”文件夹,并复制粘贴一个标准的内容为空的.mdf文件。
为方便大家学习我这里为大家提供了示例数据库
生成数据库
1、双击Startup.cs
2、右键选“ 打开所在的文件夹”
3、在controller文件夹上按 shift +右键 选“在此处打开命令窗口”
4、命令框输入cd.. 回车后退回上层目录
5、输入下面的命令
dotnet ef migrations add Initial 建立并初始化数据库
dotnet ef database update 更新数据库
dotnet ef migrations add xxxx 更新模型字段后需要执行此命令通知vs重新编译表变动 xxxx为变更的任意字段名 一个就够 系统会自动追加变更添加的其他字段
dotnet ef database update 更新数据库 或者vs中
PM>
Enable-Migrations 启动迁移配置PM> Add-Migration xxxx 更新数据库的迁移的名称
更新模型字段后需要执行此命令通知vs重新编译表变动 xxxx为变更的任意字段名 一个就够 系统会自动追加变更添加的其他字段
(注意这里必须是在Models目录中添加数据模型(类、新建项、现有项等)并重新生成后,然后添加对应的控制器和视图后才能使用此命令,生成迁移命令后马上使用Update-Database更新数据库。
)
(可以多次修改生成一次迁移命令,不能多次迁移修改却执行一次更新数据库,只能迁移一次就更新一次。)
PM> Update-Database –TargetMigration: $InitialDatabase 回滚数据库至初始状态
PM> Update-Database –TargetMigration: xxxx 回滚数据库至某个更新PM> Update-Database
更新数据库
.net 和 core2.0 数据库连接字符串的更多相关文章
- Microsoft ACE OLEDB 12.0 数据库连接字符串
Excel 97-2003 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended ...
- .Net Framework下对Dapper二次封装迁移到.Net Core2.0遇到的问题以及对Dapper的封装介绍
今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和大家分享一下.下面我会还原迁移的每一个过程 ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之七 || API项目整体搭建 6.2 轻量级ORM
更新 1.在使用的时候,特别是更新数据的时候,如果不知道哪里有问题,可以查看数据库 和 实体类 的字段,是否大小写一致,比如 name 和 Name 2.在使用Sqlsugar 的 CodeFirst ...
- .Net Core2.0下使用Dapper遇到的问题
今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和大家分享一下.下面我会还原迁移的每一个过程 ...
- Net Core2.0下使用Dapper
Net Core2.0下使用Dapper 今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和 ...
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
ADO.NET 一.ADO.NET概要 ADO.NET是.NET框架中的重要组件,主要用于完成C#应用程序访问数据库 二.ADO.NET的组成 ①System.Data → DataTable, ...
- EF--Codefirst 加密数据库连接字符串
http://www.tuicool.com/articles/QvYbEn 一.EF,CodeFirst加密SQL连接符 public LifeHelpContext() : base(" ...
- 【转】ASP.NET数据库连接字符串总结
来源:http://blog.csdn.net/lutinghuan/article/details/5973897 ASP.NET数据库连接字符串总结 一.使用OleDbConnection对象连接 ...
- web.config connectionStrings 数据库连接字符串的解释
先来看一下默认的连接SQL Server数据库配置<connectionStrings> <add name="LocalSqlServer" connect ...
随机推荐
- Windows平台下的内存泄漏检测
在C/C++中内存泄漏是一个不可避免的问题,很多新手甚至有许多老手也会犯这样的错误,下面说明一下在windows平台下如何检测内存泄漏. 在windows平台下内存泄漏检测的原理大致如下. 1. 在分 ...
- [转载自阿里丁奇]各版本MySQL并行复制的实现及优缺点
MySQL并行复制已经是老生常谈,笔者从2010年开始就着手处理线上这个问题,刚开始两三年也乐此不疲分享,现在再提这个话题本来是难免"炒冷饭"嫌疑. 最近触发再谈这个话题,是 ...
- replace into 浅析之一
一 介绍 在笔者支持业务过程中,经常遇到开发咨询replace into 的使用场景以及注意事项,这里做个总结.从功能原理,性能和注意事项上做个说明.二 原理2.1 当表中存在主键但是不存在唯一建的 ...
- linux下安装ffmpeg
1. 首先安装系统编译环境 yum install -y automake autoconf libtool gcc gcc-c++ #CentOS 2. 编译所需源码包 #yasm:汇编器,新版 ...
- 【vue系列之三】从一个vue-pdf-shower,说说vue组件和npm包
前言 从去年年初开始,自己便下决心要写一个vue系列的博客,但时至今日,才写系列的第三篇博客,想来甚是惭愧. 但是慢归慢,每一篇都要保证质量,以及要写出自己的心路历程,防止自己工作中填的坑再让读者走一 ...
- jq传统火车轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 变量、交互&注释、数字&字符串&布尔、格式化输出
变量 变量定义规范: 声明变量: name = "Neo Zheng" # name为变量名(标识符),"Neo Zheng"是变量值. 变量定义规则: ...
- 微信小程序实现按首字母检索城市列表
不说废话,上效果图 因为我有多处要用到,所以我这里是写成自定义组件的,你也可以直接改成在page页面编写: 布局左边一个scroll-view,显示城市列表,右边一个view显示字母列表,城市列表这边 ...
- Jfinal启动源码解读
本文对Jfinal的启动源码做解释说明. PS:Jfinal启动容器可基于Tomcat/Jetty等web容器启动,本文基于Jetty的启动方式做启动源码的解读和分析,tomcat类似. 入口 JF ...
- UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...