简体   繁体   中英

EF7 with identity on existing database

I had MVC4 project with Identity 2.0 that was not created automatically by using MVC template. Nothing was changed there. Then I decided to migrate to the project to the MVC6 + EF7. I've created the 1 more project in the same solution and moved all the objects there. When I am trying to run the application EF tries to create the objects that already exists so the application fails with following message:

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

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? I am using OLEDB to access database and EF is used only for the Identity.

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)'. There is no implicit reference conversion from 'Proj.Accounting.Web.Angular.Models.ApplicationDbContext' to '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

Are you using code-first? With code first EF tries to create the database. 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

Scaffold a new migration named "First"

-------------------------- EXAMPLE 2 --------------------------

C:\\PS>Add-Migration First -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/

Try to use the following methods by running the scripts on PM Console:

1) Enable migration:

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

2) Create an empty migration by using “IgnoreChanges”:

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

3) Update database:

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

For more information: https://msdn.microsoft.com/en-us/data/dn579398.aspx .

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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