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
修改前代碼
1 public class Startup 2 { 3 public Startup(IHostingEnvironment env) 4 { 5 var builder = new ConfigurationBuilder() 6 .SetBasePath(env.ContentRootPath) 7 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 8 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 9 10 if (env.IsDevelopment()) 11 { 12 // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709 13 builder.AddUserSecrets<Startup>(); 14 } 15 16 builder.AddEnvironmentVariables(); 17 Configuration = builder.Build(); 18 } 19 20 public IConfigurationRoot Configuration { get; } 21 22 // This method gets called by the runtime. Use this method to add services to the container. 23 public void ConfigureServices(IServiceCollection services) 24 { 25 // Add framework services. 26 services.AddDbContext<ApplicationDbContext>(options => 27 options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 28 29 services.AddIdentity<ApplicationUser, IdentityRole>() 30 .AddEntityFrameworkStores<ApplicationDbContext>() 31 .AddDefaultTokenProviders(); 32 33 services.AddMvc(); 34 35 // Add application services. 36 services.AddTransient<IEmailSender, AuthMessageSender>(); 37 services.AddTransient<ISmsSender, AuthMessageSender>(); 38 }
修改后代碼
1 public class Startup 2 { 3 //添加修改 聲明一個連接字符串(20行) 4 private string _contentRootPath = ""; 5 6 public Startup(IHostingEnvironment env) 7 { 8 var builder = new ConfigurationBuilder() 9 .SetBasePath(env.ContentRootPath) 10 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 11 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 12 13 //添加修改(27行) 14 _contentRootPath = env.ContentRootPath; 15 16 } 17 18 19 public void ConfigureServices(IServiceCollection services) 20 { 21 //添加修改(45行)聲明變量conn並做相應處理 22 string conn = Configuration.GetConnectionString("DefaultConnection"); 23 if(conn.Contains("%CONTENTROOTPATH%")) 24 { 25 conn = conn.Replace("%CONTENTROOTPATH%", _contentRootPath); 26 } 27 //修改默認的連接服務為conn 28 services.AddDbContext<ApplicationDbContext>(options => 29 options.UseSqlServer(conn)); 30 31 ... 32 }
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,把編譯好的程序復制到生產機器上,執行程序即可。
