0

I have problems with database in my project in .NET MVC in Visual Studio 2013. I couldn't update databse by migrations, I changed models and updated database, but nothing changed, so I deleted all databases and migrations rebuild project, but now when i type in console enable-migrations i have this

More than one context type was found in the assembly 'Ogloszenia'. To enable migrations for Ogloszenia.Models.UsersContext, use Enable-Migrations -ContextTypeName Ogloszenia.Models.UsersContext. To enable migrations for Ogloszenia.Models.Plik+BazaPlikow, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Plik+BazaPlikow. To enable migrations for Ogloszenia.Models.Skarga+BazaSkarg, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Skarga+BazaSkarg. To enable migrations for Ogloszenia.Models.SlowaZakazane+BazaSlowZakazanych, use Enable-Migrations -ContextTypeName Ogloszenia.Models.SlowaZakazane+BazaSlowZakazanych. To enable migrations for Ogloszenia.Models.Kategorie+BazaKategorii, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Kategorie+BazaKategorii. To enable migrations for Ogloszenia.Models.Ogloszenie+BazaOgloszen, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Ogloszenie+BazaOgloszen. To enable migrations for Ogloszenia.Models.Ustawienie+BazaUstawien, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Ustawienie+BazaUstawien. To enable migrations for Ogloszenia.Models.Wiadomosc+BazaWiadomosci, use Enable-Migrations -ContextTypeName Ogloszenia.Models.Wiadomosc+BazaWiadomosci.`

What do?

Jcl
  • 27,696
  • 5
  • 61
  • 92
Klapek
  • 31
  • 6
  • 1
    Do you have more than one DbContext in your project? If so, do exactly what the message is telling you – Jcl Jan 24 '15 at 17:03
  • Yes i had few contexts, but when I type what is says, it doesn't create new database – Klapek Jan 24 '15 at 17:06
  • `Enable-migrations` doesn't create any databases, it only sets up the code for migrations. You use `Update-Database` to create the database or update to a specific migration. – Jcl Jan 24 '15 at 17:08
  • Your question doesn't resemble at all the problem you are having, and with the information you provided, it's **impossible** that anyone here can help you. Please *think* your question before asking, and once written, imagine you are not the owner of that code, you have never seen that code, and someone comes and asks you that question: would you be able to help? – Jcl Jan 24 '15 at 17:13
  • @Klapek see this tutorial for multiple dbContexts: http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html and this answer: http://stackoverflow.com/a/13477276/809357 – trailmax Jan 24 '15 at 17:59

1 Answers1

-1

First of all make a separate class MigrationDbContext with all DbSets from your DbContexts.

  1. Then enable migrations for it only. Skip enable-migrations for your production DbContexts, because of EF natively doesn't support migrations for several DbContexts.
  2. Then apply command add-migration.
  3. Then apply command update-migration.

Good and quick reference for the most commonly used command names and parameters is here

rock_walker
  • 453
  • 5
  • 14
  • Ok topic closed. I found old copy of database on pendrive and it worked. I don't wanna know why it didn't work, it's my last job in VS. – Klapek Jan 24 '15 at 17:57
  • That's not true. Latest version of EF supports migrations for multiple contexts: http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html – trailmax Jan 24 '15 at 17:58
  • That is true. I checked your link. The latest EF version has no significant changes from previous one for migration of several DbContexts. The main issue in migration of several DbContexts is common DbSets, which can be met in couple of DbContexts. When *.cs migration file will be generated, you should manually comment out in `Up()` method this common Tables, which have almost been built in DataBase. In this simple tutorial the author comment out only single table, but in production it may be dozens of tables with foreign keys - that's become a hell. And it's not look like smart native support – rock_walker Jan 24 '15 at 18:25