.net 和 core 数据库连接字符串
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 数据库连接字符串的更多相关文章
- JavaScript日历控件开发 C# 读取 appconfig文件配置数据库连接字符串,和配置文件 List<T>.ForEach 调用异步方法的意外 ef 增加或者更新的习惯思维 asp.net core导入excel 一个二级联动
JavaScript日历控件开发 概述 在开篇之前,先附上日历的代码地址和演示地址,代码是本文要分析的代码,演示效果是本文要实现的效果代码地址:https://github.com/aspwebc ...
- Azure Web App (三)切换你的Net Core Web 项目的数据库连接字符串
一,引言 上一篇文章讲到今天我们演示了一下,如何在Web App中创建 “Deployment Slot”进行快速无停机部署新功能代码,也使用VS进行发布到创建的Web App中创建的新的部署槽位中, ...
- .NET跨平台之旅:数据库连接字符串写法引发的问题
最近在一个ASP.NET Core站点中遇到一个奇怪问题.当用dotnet run命令启动站点后,开始的一段时间请求执行速度超慢,有时要超过20秒,有时甚至超过1分钟,日志中会记录这样的错误: Sys ...
- EF--Codefirst 加密数据库连接字符串
http://www.tuicool.com/articles/QvYbEn 一.EF,CodeFirst加密SQL连接符 public LifeHelpContext() : base(" ...
- C# 根据ADO.NET数据库连接字符串构建EntityFrame数据库连接字符串
为了保持开发效率,以及保持代码优雅,项目中引用了EntityFrame.但是又因为某些报表功能需要大量计算,所以又要求直接使用ADO.NET,调用存储过程进行计算. 于是乎webconfig文件中就会 ...
- 【转】ASP.NET数据库连接字符串总结
来源:http://blog.csdn.net/lutinghuan/article/details/5973897 ASP.NET数据库连接字符串总结 一.使用OleDbConnection对象连接 ...
- web.config connectionStrings 数据库连接字符串的解释
先来看一下默认的连接SQL Server数据库配置<connectionStrings> <add name="LocalSqlServer" connect ...
- 获取不到app.config里面的数据库连接字符串的解决方法
今天在自己的类库里添加了对app.config文件的数据库连接字符串的引用,但是返回的居然是Null,纳闷了.然后在网上找到了答案原来是我的app.config文件加错了地方,应该加到启动项目里面,而 ...
- [转]ASP.NET数据库连接字符串总结
这个不难,而且很重要,但总忘,找了篇比较全的,作为资料. 原文链接http://developer.51cto.com/art/201107/275406.htm 关于数据库链接字符串的相关知识,我们 ...
随机推荐
- Python 工匠:善用变量来改善代码质量
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂优文发表于云+社区专栏 作者:朱雷 | 腾讯IEG高级工程师 『Python 工匠』是什么? 我一直觉得编程某种意义上是一门『手艺 ...
- google tensorflow bert代码分析
参考网上博客阅读了bert的代码,记个笔记.代码是 bert_modeling.py 参考的博客地址: https://blog.csdn.net/weixin_39470744/article/de ...
- H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
摘要:初略算了算大概有20天没有写博客了,原本是打算1月1号元旦那天写一个年终总结的,博客园里大佬们都在总结过去,迎接将来,看得我热血沸腾,想想自己也工作快2年了,去年都没有去总结一下,今年势必要总结 ...
- awk去重以某列重复的行
[root@localhost cc]# cat 2.txt adc 3 5 a d aa 3 adfa d ba 3 adf 去重第一列重复的行: [root@localhost cc]# cat ...
- [转]查看SQL Server被锁的表以及如何解锁
本文转自:https://www.cnblogs.com/shy1766IT/p/6225694.html 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOC ...
- WCF配置文件与文件下载之坎坷路
题外话:本以为我会WCF了,精通WCF了,毕竟刚做过一个WCF的项目,不就是写写契约接口,然后实现接口,改下配置.最后用控制台或者服务发布一下,不就能用了.不就是简单ABC吗?不是So Easy吗?做 ...
- vs2013中,自定义mvc 添加视图脚手架
参考文章: http://weblogs.asp.net/imranbaloch/archive/2013/09/15/customizing-the-asp-net-mvc-5-web-api-2- ...
- 线程5--GCD简介
/******************************************************/ 同步函数 (1)并发队列:不会开线程 (2)串行队列:不会开线程 异步函数 ...
- 宜立方商城中,mvn报错'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:spring-webmvc:jar报错
'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:s ...
- 使用google-gson类库解析json文件
使用google-gson类库解析json文件 使用JsonParser解析器来解析字符串和输入流,变成json对象 代码如下: public class Readjson { public stat ...