Visual Studio 2017 使用 EF Core 更改 mdf 文件的本地数据库默认位置 before/after 迁移
Visual Studio 2017 using EF Core change local database default location for mdf file before/after migration
我的问题标题可能需要帮助。所以这里有更多细节。
在 VS2015 中,我在使用代码优先迁移创建数据库时,在完成我的第一次迁移后,我可以通过转到 App_Data 文件夹并单击 MDF 文件来在 VS 中查看数据库。然后它将在服务器资源管理器中打开。
但是,在 VS2017 中,我不再有 App_Data 文件夹,当我进行第一次迁移时,我花了一些时间来找出我的 MDF 文件所在的位置。
如果你不知道这里是我用来找到它的步骤:
完成 add-migration 和 update-database 之后。
转到查看->SQL服务器Object资源管理器
在 SQL 服务器下寻找您 (localdb)\MSSQLLocalDB->Databases-> 您的数据库名称应该显示在您的 appsettings.json 连接字符串中。可能以 aspnet-[db name]-[bunch of numbers and letters]
开头
我右键单击此数据库并转到属性。
"Current Connection Parameters" 下是 MDF 文件的路径。
当前位置是"C:/user/username"
所以我的问题是如何将其设置为不同的默认位置?我希望我的 MDF 文件显示在数据文件夹或项目文件夹中的类似文件夹中。
如果有人问过这个问题。抱歉,我尝试改写我的问题大约 20 次,以确定是否有人已经问过它。如果这个问题已经以我不熟悉的方式被问到,我会很快删除这个问题。
好的,所以对于 Entity Framework Core,它涉及的更多一些。您可以在 Visual Studio(或 Sql Management Studio)中的 SQL Server Object Explorer
中打开您的数据库,并使用 SQL 查询在您想要的位置创建数据库。
create database test on (name='test', filename='c:\Projects\test.mdf');
然后像通常在连接字符串中那样使用 (LocalDb) 引用它:
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=test;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
然后此测试正确运行
Program.cs
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
var context = new DbContext(optionsBuilder.Options);
context.Database.EnsureCreated();
}
}
}
所以您仍在使用同一台服务器,但您将数据库放在您想要的文件夹中。
在行动中:
您必须在连接字符串中使用 AttachDbFileName,例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString: @"data source=.;AttachDbFileName=d:\data\yourDBname.mdf ;initial catalog=yourDBname;persist security info=True;user id=sa;password=1234;MultipleActiveResultSets=True;App=EntityFramework");
base.OnConfiguring(optionsBuilder);
}
我的问题标题可能需要帮助。所以这里有更多细节。
在 VS2015 中,我在使用代码优先迁移创建数据库时,在完成我的第一次迁移后,我可以通过转到 App_Data 文件夹并单击 MDF 文件来在 VS 中查看数据库。然后它将在服务器资源管理器中打开。
但是,在 VS2017 中,我不再有 App_Data 文件夹,当我进行第一次迁移时,我花了一些时间来找出我的 MDF 文件所在的位置。
如果你不知道这里是我用来找到它的步骤:
完成 add-migration 和 update-database 之后。
转到查看->SQL服务器Object资源管理器
在 SQL 服务器下寻找您 (localdb)\MSSQLLocalDB->Databases-> 您的数据库名称应该显示在您的 appsettings.json 连接字符串中。可能以 aspnet-[db name]-[bunch of numbers and letters]
开头我右键单击此数据库并转到属性。 "Current Connection Parameters" 下是 MDF 文件的路径。
当前位置是"C:/user/username"
所以我的问题是如何将其设置为不同的默认位置?我希望我的 MDF 文件显示在数据文件夹或项目文件夹中的类似文件夹中。
如果有人问过这个问题。抱歉,我尝试改写我的问题大约 20 次,以确定是否有人已经问过它。如果这个问题已经以我不熟悉的方式被问到,我会很快删除这个问题。
好的,所以对于 Entity Framework Core,它涉及的更多一些。您可以在 Visual Studio(或 Sql Management Studio)中的 SQL Server Object Explorer
中打开您的数据库,并使用 SQL 查询在您想要的位置创建数据库。
create database test on (name='test', filename='c:\Projects\test.mdf');
然后像通常在连接字符串中那样使用 (LocalDb) 引用它:
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=test;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
然后此测试正确运行
Program.cs
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
var context = new DbContext(optionsBuilder.Options);
context.Database.EnsureCreated();
}
}
}
所以您仍在使用同一台服务器,但您将数据库放在您想要的文件夹中。
在行动中:
您必须在连接字符串中使用 AttachDbFileName,例如:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString: @"data source=.;AttachDbFileName=d:\data\yourDBname.mdf ;initial catalog=yourDBname;persist security info=True;user id=sa;password=1234;MultipleActiveResultSets=True;App=EntityFramework");
base.OnConfiguring(optionsBuilder);
}