代码第一次迁移更新在localdb上错误的Db [英] Code First migrations updating the wrong Db on localdb
问题描述
我有一个DbContext,它位于解决方案的另一个项目中。
public class EFDbContext:DbContext
{
public DbSet< Book>书籍{get;组; }
public DbSet< Author>作者{get;组; }
public DbSet< Publisher>出版商{get;组;
}
当我启用迁移和添加迁移时 [name] 即可。似乎创建自己的数据库名为
[foo-projectname] .Domain.Concrete.EFDbContext
而不是附加到我创建的数据库,名为 [foo-projectname] 。
在网站的webconfig中,连接字符串由目录 [foo-projectname] 引用。
如果我更改webconfig到其他数据库,然后我从我添加到该数据库的项目中获取结果。但是我想使用我创建的数据库。
我不想坚持这个自动创建的原因是因为我不确定迁移到SqlServer,不想进一步陷入困境。我也收到错误
支持EFDbContext上下文的模型自...更改后
即使我没有改变任何东西。
X是包含派生 DbContext
类的项目名称。 / p>
- 确保
$ Manager $控制台
/ code>是X
- 确保解决方案上的活动启动项目是X
$ b $另一个解决方法是在运行迁移语法时提供以下参数。
-ProjectName X -StartUpProjectName X
一些参数说明:
-ProjectName< String>
指定包含迁移配置类型为
的项目。如果省略,则使用在包管理器控制台
中选择的默认项目。
-StartUpProjectName< String>
指定用于命名连接字符串的配置文件。如果省略
,则使用指定的项目的配置文件。
-ConfigurationTypeName< String>
指定要使用的迁移配置。如果省略,迁移将
尝试在目标
项目中查找单个迁移配置类型。
-ConnectionStringName< String>
从应用程序的
配置文件中指定要使用的连接字符串的名称。
-ConnectionString< String>
指定要使用的连接字符串。如果省略,将使用上下文的
默认连接。
I am using code first migrations to create and update the database for my asp mvc site.
I have a DbContext, which is in another project in the solution.
public class EFDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Publisher> Publishers { get; set; }
}
When i enable-migrations and add-migration [name]. It seems to create its own db called
[foo-projectname].Domain.Concrete.EFDbContext
Instead of the attaching to the database i created, called [foo-projectname]. In the webconfig for the site the connection string is referencing by catalog "[foo-projectname]".
If i change the webconfig to the other database , then i get results back from items i have added into the that database. However i want to use the database that i created.
The reason i do not want to stick with this auto created one is because i am not sure on the migration to SqlServer and do not want to get stuck further down the line. I am also getting the error
The model backing the 'EFDbContext' context has changed since...
even when i have not changed anything.
X is the project name that contains derived DbContext
class.
- Make sure the
Default project
onPackage Manager Console
is X - Make sure the active start up project on the solution is X
Another workaround is, provide following parameters when running the migration syntax.
-ProjectName X -StartUpProjectName X
Some Parameters Description:
-ProjectName <String>
Specifies the project that contains the migration configuration type to be
used. If omitted, the default project selected in package manager console
is used.
-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If
omitted, the specified project's configuration file is used.
-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will
attempt to locate a single migrations configuration type in the target
project.
-ConnectionStringName <String>
Specifies the name of a connection string to use from the application's
configuration file.
-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context's
default connection will be used.
这篇关于代码第一次迁移更新在localdb上错误的Db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!