使用Entity Framework迁移时如何显式命名数据库4.3 [英] How to explicitly name the database when using Entity Framework Migrations 4.3

查看:120
本文介绍了使用Entity Framework迁移时如何显式命名数据库4.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用Entity Framework迁移,并注意到,当我运行 Update-Database 命令时,数据库名称不会为我提供。



我的连接字符串是:

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

我第一次运行更新数据库时,我的数据库是用正确的名称​​ TestDB 。但是,一旦我更改了我的一个实体,除非我添加一个启动项目名称(我正在使用多项目解决方案),否则它不会再更新:

 更新数据库-StartUpProjectNameTestDB.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;组; }
public DbSet< Table2> Table2 {get;组; }

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

谢谢,



Rich

解决方案

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



分割时在多个项目中应用程序,运行应用程序时使用的连接字符串是项目启动的一个。当迁移时,使用的连接字符串是包含迁移的项目。



当我进行了类似的设置时,我不得不在两个地方添加连接字符串。有点尴尬,但它有效。


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

解决方案

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.

这篇关于使用Entity Framework迁移时如何显式命名数据库4.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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