简体   繁体   English

在现有数据库上具有标识的 EF7

[英]EF7 with identity on existing database

I had MVC4 project with Identity 2.0 that was not created automatically by using MVC template.我有一个带有 Identity 2.0 的 MVC4 项目,它不是使用 MVC 模板自动创建的。 Nothing was changed there.那里什么都没有改变。 Then I decided to migrate to the project to the MVC6 + EF7.然后我决定把项目迁移到MVC6+EF7。 I've created the 1 more project in the same solution and moved all the objects there.我在同一个解决方案中创建了另外 1 个项目,并将所有对象移动到那里。 When I am trying to run the application EF tries to create the objects that already exists so the application fails with following message:当我尝试运行应用程序时,EF 尝试创建已经存在的对象,因此应用程序失败并显示以下消息:

SqlException: There is already an object named 'AspNetRoles' in the database.

I've tried to google it and found the proposal to run:我试图用谷歌搜索它并找到运行的建议:

Add-Migration Initial -IgnoreChanges添加迁移初始 -IgnoreChanges

Then I am getting following error message:然后我收到以下错误消息:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in 
assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not 
marked as serializable."
At C:\Users\user\Source\Repos\Proj.Accounting\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720 char:5
+     $domain.SetData('startUpProject', $startUpProject)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SerializationException

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project, Int32 shellVersion)
   at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project)
   at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.

So my question is: how to disable EF migrations?所以我的问题是:如何禁用 EF 迁移? I am using OLEDB to access database and EF is used only for the Identity.我正在使用 OLEDB 访问数据库,而 EF 仅用于身份。

UPDATE: by suggested answer below I've tried to add following line更新:根据下面的建议答案,我尝试添加以下行

Database.SetInitializer<ApplicationDbContext>(null);

to the Startup class constructor启动类构造函数

Severity Code Description Project File Line Error CS0311 The type 'Proj.Accounting.Web.Angular.Models.ApplicationDbContext' cannot be used as type parameter 'TContext' in the generic type or method 'Database.SetInitializer(IDatabaseInitializer)'.严重性代码描述项目文件行错误 CS0311 类型“Proj.Accounting.Web.Angular.Models.ApplicationDbContext”不能用作泛型类型或方法“Database.SetInitializer(IDatabaseInitializer)”中的类型参数“TContext”。 There is no implicit reference conversion from 'Proj.Accounting.Web.Angular.Models.ApplicationDbContext' to 'System.Data.Entity.DbContext'.没有从“Proj.Accounting.Web.Angular.Models.ApplicationDbContext”到“System.Data.Entity.DbContext”的隐式引用转换。 Proj.Accounting.Web.Angular.DNX 4.5.1 C:\\Users\\User\\Source\\Repos\\Proj.Accounting\\Proj.Accounting.Web.Angular\\Startup.cs 50 Proj.Accounting.Web.Angular.DNX 4.5.1 C:\\Users\\User\\Source\\Repos\\Proj.Accounting\\Proj.Accounting.Web.Angular\\Startup.cs 50

Are you using code-first?您是否使用代码优先? With code first EF tries to create the database.首先使用代码,EF 尝试创建数据库。 If that database exist you could get those errors.如果该数据库存在,您可能会收到这些错误。 Try to disable code-first or set initial database.尝试禁用代码优先或设置初始数据库。

disable code-first 禁用代码优先

set initial database 设置初始数据库

Or you can try using the package console:或者您可以尝试使用包控制台:

C:\\PS>Add-Migration First C:\\PS>首先添加迁移

Scaffold a new migration named "First"脚手架一个名为“First”的新迁移

-------------------------- EXAMPLE 2 -------------------------- -------------------------- 示例 2 --------------- ----

C:\\PS>Add-Migration First -IgnoreChanges C:\\PS>首先添加迁移-IgnoreChanges

Scaffold an empty migration ignoring any pending changes detected in the current model.忽略在当前模型中检测到的任何未决更改,构建一个空迁移。 This can be used to create an initial, empty migration to enable Migrations for an existing database.这可用于创建初始的空迁移,以便为现有数据库启用迁移。 NB Doing this assumes that the target database schema is compatible with the current model.注意这样做假设目标数据库模式与当前模型兼容。

https://coding.abel.nu/2012/03/ef-migrations-command-reference/ https://coding.abel.nu/2012/03/ef-migrations-command-reference/

Try to use the following methods by running the scripts on PM Console:尝试通过在 PM 控制台上运行脚本来使用以下方法:

1) Enable migration: 1)启用迁移:

enable-migrations -ContextTypeName ProjectName.DbContext -MigrationsDirectory:Migrations -EnableAutomaticMigrations –Force

2) Create an empty migration by using “IgnoreChanges”: 2)使用“IgnoreChanges”创建一个空迁移:

Add-Migration –configuration ProjectName.Migrations.Configuration migration_01 –IgnoreChanges

3) Update database: 3)更新数据库:

Update-Database -configuration ProjectName.Migrations.Configuration -Verbose

For more information: https://msdn.microsoft.com/en-us/data/dn579398.aspx .有关更多信息: https : //msdn.microsoft.com/en-us/data/dn579398.aspx

Hope this helps...希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM