在迁移之前/之后,使用EF Core的Visual Studio 2017更改mdf文件的本地数据库默认位置 [英] Visual Studio 2017 using EF Core change local database default location for mdf file before/after migration

查看:267
本文介绍了在迁移之前/之后,使用EF Core的Visual Studio 2017更改mdf文件的本地数据库默认位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题标题可能需要帮助。所以这里有更多细节。

I may need help with the title of my question. So here is more detail.

在VS2015中,使用代码首次迁移创建数据库时,在完成第一次迁移后,我可以通过转到App_Data文件夹并单击来查看VS中的数据库MDF文件。然后,它将在服务器资源管理器中打开。

In VS2015 I when creating a database using code first migrations, After completing my first migration I was able to view the database in VS by going to the App_Data folder and clicking on the MDF file. It would then open in Server Explorer.

但是,在VS2017中,我不再有App_Data文件夹,当我进行第一次迁移时,它花费了一些时间来确定MDF文件的位置。

However, in VS2017 I no longer have an App_Data folder and when I did my first migration it took a bit of work to find out where my MDF file was located.

如果您不知道这是我以前找到它的步骤:

If you don't know here are the steps I used to locate it:

完成添加后-迁移和更新数据库。

After doing the add-migration and update-database.

转到视图-> SQL Server对象资源管理器

Go to View->SQL Server Object Explorer

在SQL Server外观下为您(localdb)\ MSSQLLocalDB->数据库->数据库名称应显示在appsettings.json连接字符串中。可能以aspnet- [数据库名称]-[数字和字母束]开头。

Under SQL Server look for you (localdb)\MSSQLLocalDB->Databases->name of your databases should show in your appsettings.json connection string. Probably starts with aspnet-[db name]-[bunch of numbers and letters]

我右键单击该数据库并转到属性。
在当前连接参数下是MDF文件的路径。

I right clicked on this database and went to properties. Under "Current Connection Parameters" is the path to the MDF file.

当前位置是 C:/用户/用户名

The current location is "C:/user/username"

所以我的问题是如何设置这到其他默认位置?我希望将MDF文件显示在Data文件夹或项目文件夹中的类似文件中。

So my question is how do I set this to a different default location? I would like my MDF files to show in the Data folder or something similar that is in my project folder.

如果已问过此问题。抱歉,我尝试将问题重新措词约20次,以弄清楚是否有人已经提出过。如果已经以我不熟悉的方式提出了这个问题,我将迅速删除它。

If this question has been asked. I apologize I tried rewording my question about 20 times to figure out if someone already asked it. I will quickly remove this question if it has already been asked in a way that is unfamiliar to me.

推荐答案

好,所以对于Entity Framework Core,它涉及更多。您可以在Visual Studio(或Sql Management Studio)中的 SQL Server对象资源管理器中打开数据库,并使用SQL查询在所需的数据库中创建数据库。

Ok, so for Entity Framework Core, it is a bit more involved. You can open your db in SQL Server Object Explorer in Visual Studio (or in Sql Management Studio) and create your database where you want it using a SQL query.

create database test on (name='test', filename='c:\Projects\test.mdf');

然后使用(LocalDb)引用它,就像通常在连接字符串中一样:

And then reference it using (LocalDb) the way you normally would in the connection string:

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();
        }
    }
}

所以您仍在使用

实际操作:

In action:

这篇关于在迁移之前/之后,使用EF Core的Visual Studio 2017更改mdf文件的本地数据库默认位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆