使用 Entity Framework Migrations 4.3 时如何显式命名数据库

发布于 2025-01-06 04:00:46 字数 1487 浏览 1 评论 0原文

我最近开始使用实体框架迁移,并注意到当我运行 Update-Database 命令时,数据库名称没有为我提取。

我的连接字符串是:

<connectionStrings>
<add name="DataContext" connectionString="Server=.\SQLEXPRESS;Initial Catalog=TestDB;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
</connectionStrings>

我第一次运行 Update-Database 时,我的数据库是使用正确的名称 TestDB 创建的。但是,一旦我对其中一个实体进行更改,它就不会再为我更新,除非我添加启动项目名称(我正在使用多项目解决方案):

Update-Database -StartUpProjectName "TestDB.Data"

这将创建另一个新数据库,迁移将一直继续使用。我不介意必须输入 StartUpProjectName 命令,但是有没有办法覆盖生成的数据库的默认名称?它始终将数据库创建为是否

TestDB.Data.DataContext

有一种方法可以确保传递 StartUpProject 名称时创建的数据库仅称为 TestDB 或者这是使用 StartUpProjectName 设置的限制?

请注意,我认为我需要指定 StartUpProjectName 的原因是我有一个多层项目设置。迁移配置文件位于我的“数据”项目中,实体/模型位于我的“域”项目中,等等。我的 Global.asax.cs 文件中目前也没有任何初始化选项,就像我之前使用的那样首先编写代码 ef 4.2。因此,在我的项目中,我的数据项目中只有一个 DataContext,该项目中也有一个迁移配置。

编辑:

自从我最初设置这个问题以来,我偶然发现了在多项目解决方案中命名数据库的“正确”方法。虽然下面的答案可行,但它确实意味着您正在另一个区域复制 web.config,这不是理想的解决方案。相反,您可以通过执行类似的操作将名称放入 DbContext (DataContext 只是我在项目中使用的名称):

public class DataContext : DbContext
{
    public DataContext() : base("DatabaseNameHere")
    { }

    public DbSet<Table1> Table1 { get; set; }
    public DbSet<Table2> Table2 { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }
}

谢谢,

Rich

I've recently started using Entity Framework migrations and noticed that the database name is not pulling through for me when I run the Update-Database command.

My connectionstring is:

<connectionStrings>
<add name="DataContext" connectionString="Server=.\SQLEXPRESS;Initial Catalog=TestDB;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" />
</connectionStrings>

The very first time I run Update-Database my database is created with the correct name TestDB. However, as soon as I make a change to one of my entities it will not update any longer for me unless I add a Start Up Project Name (I'm using a multi project solution):

Update-Database -StartUpProjectName "TestDB.Data"

This then makes another new database which migrations will always continue to use. I don't mind having to put in the StartUpProjectName command but is there a way to override the default name for the database this produces? It always creates the database as

TestDB.Data.DataContext

Is there a way to ensure that the database created when passing the StartUpProject name is just called TestDB or is this a limitation of using the StartUpProjectName setting?

As a note, I think the reason I need to specify the StartUpProjectName is that I have a multilayer project setup. The Migrations Configuration file is in my 'Data' project, the entities/models are in my 'Domain' project, etc. I also do not currently have any initialize options in my Global.asax.cs file as I would have used previously on code first ef 4.2. So in my project I just have a DataContext in my Data project and the Migrations Configuration in that project also.

EDIT:

Since I originally setup this question I stumbled onto the 'correct' way to name a database in a multiproject solution. While the answer below will work it does mean you are duplicating your web.config in another area which isn't an ideal solution. Instead you can just put the name into your DbContext by doing something like this (DataContext is just the name I used in my project):

public class DataContext : DbContext
{
    public DataContext() : base("DatabaseNameHere")
    { }

    public DbSet<Table1> Table1 { get; set; }
    public DbSet<Table2> Table2 { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }
}

Thanks,

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光无声 2025-01-13 04:00:46

您可以通过将其作为参数提供来避免在 app.config 中管理它:

Update-Database -Verbose 
 -ConnectionString "CONNECTIONSTRING" 
 -ConnectionProviderName "System.Data.SqlClient"
 -StartupProjectName WEBSITE_PROJECT -ProjectName MIGRATION_PROJECT

如果您喜欢无休止地输入,则 Easy-piezy。

You can avoid managing it in app.config by offering it as a parameter:

Update-Database -Verbose 
 -ConnectionString "CONNECTIONSTRING" 
 -ConnectionProviderName "System.Data.SqlClient"
 -StartupProjectName WEBSITE_PROJECT -ProjectName MIGRATION_PROJECT

Easy-piezy, if you love to type endlessly.

著墨染雨君画夕 2025-01-13 04:00:46

在执行update-database 时,您应该指定包含迁移的项目。确保该项目中有一个包含正确连接字符串的 app.config 文件。

将应用程序拆分为多个项目时,运行应用程序时使用的连接字符串是启动的项目之一。迁移时,使用的连接字符串是包含迁移的项目之一。

当我进行类似的设置时,我必须在两个位置添加连接字符串。有点尴尬,但它有效。

When doing update-database you should specify the project that contains the migrations. Make sure that you have an app.config file in that project that contains the correct connection string.

When splitting up an application over several projects, the connection string used when running the app is the one of the project started. When migrating, the connection string used is the one of the project containing the migrations.

When I did a similar setup I had to add the connection string in two places. A bit awkward, but it works.

甜中书 2025-01-13 04:00:46

您可以将连接字符串存储在网站项目的 web.config 中以及另一个项目中的 DBContext 和迁移文件中,并且仍然共享相同的连接字符串。但是,您需要确保除了将数据项目(或其中包含 DBContext 等的任何项目)设置为包管理器控制台的默认项目外,您还需要确保您的网站已设置到默认启动项目!!!

我在任何地方都看不到这个记录,但是疯狂的 24 小时无法弄清楚为什么我的迁移突然被应用到 SQLExpress 数据库,导致我得出了这个结论。

You can have your connection string stored in the web.config in your website project and the DBContext and migration files in another project and still share the same connection string. However you need to make sure that as well as setting the Data project (or whatever project has the DBContext etc. in it) as the default project for the Package Manager Console, you ALSO need to make sure that your website is set to the Default StartUp Project!!!

I cannot see this documented anywhere, but a frantic 24 hours of not being able to figure out why my migrations where suddenly being applied to a SQLExpress db, led me to this conclusion.

々眼睛长脚气 2025-01-13 04:00:46

我尝试使用 Nuget 的最新 EF5。

但是 Update-Database 不会从包含迁移的项目中读取 App.config (就像 1 年前的答案一样),但它只会读取 * .config 来自启动项目。这很棒,但我发现 Add-MigrationUpdate-Database 如何在这里找到合适的连接字符串:

  1. 它尝试首先获取“DefaultConnection”连接字符串
  2. 然后它尝试根据上下文类名称获取连接字符串名称。例如,我有从 DbContext 派生的 MyContext 类,因此我可以使用“MyContext”连接字符串名称。当我有多个数据库连接时很有用。
  3. 如果未找到上述两个连接字符串名称,它将失败并且不显示“DefaultConnection”连接字符串,除非您提供 -ConnectionStringName 参数。请参阅 get-help Update-Database 以查看包管理器控制台中的帮助页面。

没有重试或回退尝试,因此如果“DefaultConnection”包含错误的连接字符串,它只会显示错误。

如果连接字符串中同时存在 DefaultConnection 和上下文名称,则 DefaultConnection 将优先。

我希望 #2 成为第一次尝试,因为名称更具体,但上述步骤是 EF5 迁移在尝试连接到数据库时执行的操作。

I tried with Latest EF5 from Nuget.

However Update-Database does not read the App.config from the project that contain the migrations (just like the answer 1 year ago) but it will only read *.config from start up project. It is great but I discover how Add-Migration and Update-Database find a suitable connection string here:

  1. It trying to get "DefaultConnection" connection string first
  2. Then it is trying to get the connection string name based on context class name. E.g. I have the MyContext class derived from DbContext so I can use the "MyContext" connection string name. Useful when I have multiple db connections.
  3. If both the above connection string names are not found, it will fail and show no "DefaultConnection" connection string unless you supply the -ConnectionStringName parameter. See get-help Update-Database to view the help page in the Package Manager Console.

There is no retry or fallback attempt, so if the "DefaultConnection" contains a wrong connection string, it will simply show an error.

If both DefaultConnection and context name exist in the connection strings, DefaultConnection will take precedence.

I would prefer #2 become the first try because the name is more specific but the above steps is what EF5 Migrations do when trying to connect to the db.

聊慰 2025-01-13 04:00:46

对于 dotnet 7,以上方法都不适合我。

相反,我明确指定连接字符串作为参数。

例如

update-database -Connection "你的连接字符串"

我更喜欢这种方式,因为很清楚我使用什么连接字符串,特别是,如果你像我一样在生产中更新数据库,你可能不想猜测加载了哪个连接字符串。

With dotnet 7, non of the above worked for me.

Instead, I explicitly specified the connection string as an argument.

e.g.

update-database -Connection "your connection string"

I prefer this way because it is clear what connection string I use, in particular, if you update the database in Production like me, you may not want to guess which connection string is loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文