Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-xxxx.mdf;Initial Catalog=aspnet-xxxx;Integrated Security=True" providerName="System.Data.SqlClient"

Server=(localdb)\\mssqllocaldb; Database=xxxxx; Serve=服务器名;Database=数据库名

Server=(localdb)\\mssqllocaldb; AttachDbFilename=xxxx.mdf;Initial Catalog=xxxxx; Serve指服务器名;AttachDbFilename指连接的本地那个数据库文件,Initial Catalog指数据库名称

“AttachDbFilename=|DataDirectory|\\data.mdf”      “|DataDirectory|”代表ASP.NET项目里自动创建的App_Data文件夹并在其内创建data.mdb文件。

integrated security=true 采用集成验证

Trusted_Connection=True; 采用信任连接;

MultipleActiveResultSets=true  指定此数据库连接是否复用数据库内已建立的相同用户的连接。如为True时,建立数据库连接时会先查询服务器上是否已为此用户建立连接,如已建立则直接复用此连接。数据库的打开与关闭是很消耗系统的性能,利用这种对链接的关联方式可以减轻系统的负担。

Encrypt=False;是否加密;

TrustServerCertificate=True;设置为“true”以指定 适用于 SQL Server 的 Microsoft JDBC Driver 将不会验证 SQL Server SSL 证书。如果为“true”,当使用 SSL 加密通信层时,将自动信任 SQL Server SSL 证书。如果为“false”,适用于 SQL Server 的 Microsoft JDBC Driver 将验证服务器 SSL 证书。 如果服务器证书验证失败,驱动程序将引发错误并终止连接。 默认值为“false”。 当且仅当 encrypt 属性设置为“true”时,此属性仅影响服务器 SSL 证书验证。

AplicationIntent= ReadWrite;用来标记客户端发送来的请求类型(ApplicationIntent = ReadOnly)

Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-xxxx.mdf;Initial Catalog=aspnet-xxxx;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"

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(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment())
{
// For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets<Startup>();
} builder.AddEnvironmentVariables();
Configuration = builder.Build();
} public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// 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>();
}

修改后代码

 public class Startup
{
//添加修改 声明一个连接字符串(20行)
private string _contentRootPath = ""; public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); //添加修改(27行)
_contentRootPath = env.ContentRootPath; } public void ConfigureServices(IServiceCollection services)
{
//添加修改(45行)声明变量conn并做相应处理
string conn = Configuration.GetConnectionString("DefaultConnection");
if(conn.Contains("%CONTENTROOTPATH%"))
{
conn = conn.Replace("%CONTENTROOTPATH%", _contentRootPath);
}
//修改默认的连接服务为conn
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(conn)); ...
}

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 更新数据库 由LocalDb 数据库升级为 MSSQLSERVER 数据库
1、为了便于管理数据库还是使用AttachDbFilename=|DataDirectory|\aspnet-xxxx.mdf(相对路径)
2、只启用MSSQLSERVER的数据引擎。
3、修改步骤:
  打开App.config文件;
  修改<entityFramework>中的
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
改为如下
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 并把<parameters>节注释掉,如下

!--<parameters>
<parameter value="mssqllocaldb" />
</parameters>-->

4、程序重新编译好。

5、生产机器上安装MSSQLSERVER,把编译好的程序复制到生产机器上,执行程序即可。

 

.net 和 core 数据库连接字符串的更多相关文章

  1. JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List<T>.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动

    JavaScript日历控件开发   概述 在开篇之前,先附上日历的代码地址和演示地址,代码是本文要分析的代码,演示效果是本文要实现的效果代码地址:https://github.com/aspwebc ...

  2. Azure Web App (三)切换你的Net Core Web 项目的数据库连接字符串

    一,引言 上一篇文章讲到今天我们演示了一下,如何在Web App中创建 “Deployment Slot”进行快速无停机部署新功能代码,也使用VS进行发布到创建的Web App中创建的新的部署槽位中, ...

  3. .NET跨平台之旅:数据库连接字符串写法引发的问题

    最近在一个ASP.NET Core站点中遇到一个奇怪问题.当用dotnet run命令启动站点后,开始的一段时间请求执行速度超慢,有时要超过20秒,有时甚至超过1分钟,日志中会记录这样的错误: Sys ...

  4. EF--Codefirst 加密数据库连接字符串

    http://www.tuicool.com/articles/QvYbEn 一.EF,CodeFirst加密SQL连接符 public LifeHelpContext() : base(" ...

  5. C# 根据ADO.NET数据库连接字符串构建EntityFrame数据库连接字符串

    为了保持开发效率,以及保持代码优雅,项目中引用了EntityFrame.但是又因为某些报表功能需要大量计算,所以又要求直接使用ADO.NET,调用存储过程进行计算. 于是乎webconfig文件中就会 ...

  6. 【转】ASP.NET数据库连接字符串总结

    来源:http://blog.csdn.net/lutinghuan/article/details/5973897 ASP.NET数据库连接字符串总结 一.使用OleDbConnection对象连接 ...

  7. web.config connectionStrings 数据库连接字符串的解释

    先来看一下默认的连接SQL Server数据库配置<connectionStrings>   <add name="LocalSqlServer" connect ...

  8. 获取不到app.config里面的数据库连接字符串的解决方法

    今天在自己的类库里添加了对app.config文件的数据库连接字符串的引用,但是返回的居然是Null,纳闷了.然后在网上找到了答案原来是我的app.config文件加错了地方,应该加到启动项目里面,而 ...

  9. [转]ASP.NET数据库连接字符串总结

    这个不难,而且很重要,但总忘,找了篇比较全的,作为资料. 原文链接http://developer.51cto.com/art/201107/275406.htm 关于数据库链接字符串的相关知识,我们 ...

随机推荐

  1. 通过六个题目彻底掌握String笔试面试题

    http://blog.csdn.net/chj97/article/details/6899598 1 public static void main(String[] args) { String ...

  2. VM CentOS 问题汇总

    1. 锁定文件失败 / 模块启动失败 如下图问题: 原因分析: 虚拟机为了防止有多虚拟机共用一个虚拟磁盘(就是后 缀为.vmdk那个文件)造成数据的丢失等问题,每次启动虚拟机时会给每个虚拟磁盘加一个磁 ...

  3. Asterist搭建步骤

    环境: # cat /proc/version Linux version 2.6.18-308.el5 (mockbuild@x86-010.build.bos.redhat.com) (gcc v ...

  4. 使用DateTime的ParseExact方法实现特殊日期时间的方法详解(转)

    本篇文章是对使用DateTime的ParseExact方法实现特殊日期时间的方法进行了详细的分析介绍,需要的朋友参考下 今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象: [ ...

  5. 安装 wordpress 出现 抱歉,我不能写入wp-config.php文件

    本文告诉大家如何安装 wordpress ,在安装过程出现 抱歉,我不能写入wp-config.php文件如何解决 下载 wordpress China 简体中文 - WordPress 安装 在安装 ...

  6. CentOS普通用户没有sudo权限

    sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等.这样不仅减少了root用户的登录 和管理时间,同样也提高了安全 ...

  7. K:Treap(堆树)

      Treap=Tree+Heap.Treap是一棵二叉排序树,它的左子树和右子树分别是一个Treap,和一般的二叉排序树不同的是, Treap记录一个额外的数据, 就是优先级.Treap在以关键码构 ...

  8. MUI框架 选择器的使用

    js.css引用 <script type="text/javascript" src="librarys/mui/js/mui.min.js">& ...

  9. LINUX创建LVM、PV、VG、LV ORACLE服务器方案划分

    为裸盘分区 查看硬盘分区 fdisk -l 进入分区管理 fdisk /dev/sda 创建PV 创建PV pvcreate /dev/sda1 pvcreate /dev/sdb1 pvcreate ...

  10. PHP中文件操作(1)--打开/读取文件

    1.打开文件(fopen) 语法:resource  $fp=fopen(文件地址,模式),返回的是文件指针(file pointer) 模式 含义 r 只读 w 写(清空重写) a 追加 $fp = ...