-1

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

Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
  • Does your `ApplicationDbContext` inherit from `DbContext`? – Jcl Mar 19 '16 at 07:49
  • seems that no: public class ApplicationDbContext : IdentityDbContext – Dmitrij Kultasev Mar 19 '16 at 07:50
  • `IdentityDbContext` (if in `Microsoft.AspNet.Identity.EntityFramework`) should inherit from `System.Data.Entity.DbContext`, so that should be ok... then again, I'm not sure for DNX and ASP.Net Core, maybe you need a different NuGet package? All the different nuget packages and versioning are a serious mess right now, and documentation is scarce :-/ – Jcl Mar 19 '16 at 07:53

2 Answers2

0

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. N.B. Doing this assumes that the target database schema is compatible with the current model.

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

Community
  • 1
  • 1
Jeroen Doppenberg
  • 1,558
  • 1
  • 10
  • 13
0

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.

Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
  • enable-migrations : The term 'enable-migrations' is not recognized as the name of a cmdlet – Dmitrij Kultasev Mar 20 '16 at 19:41
  • @sidux Have a look at my answer on [The term 'Update-Database' is not recognized as the name of a cmdlet](http://stackoverflow.com/questions/9674983/the-term-update-database-is-not-recognized-as-the-name-of-a-cmdlet/29031669#29031669). – Murat Yıldız Mar 20 '16 at 19:48
  • @sidux Have you solved *"The term 'Update-Database' is not recognized as the name of a cmdlet."* problem yet? If so, what is the current problem now? – Murat Yıldız Mar 20 '16 at 19:56
  • No, I haven't. I still have the same issue. – Dmitrij Kultasev Mar 20 '16 at 19:56
  • Exit Visual Studio and try to clean the WebSiteCache folder (you may find it at *C:\Users\%USERNAME%\AppData\Local\Microsoft\WebSiteCache)* and the "Temporary ASP.NET Files" folder (find it at *C:\Users\%USERNAME%\AppData\Local\Temp\Temporary ASP.NET Files)*. Then restart your computer and continue to apply the steps you encounter the error. – Murat Yıldız Mar 20 '16 at 20:00