The first code for the first NuGet package cannot be executed from the main application

I created a module to be included in the main MVC3 web application. The module is packaged in a NuGet package and can be installed and uninstalled using NuGet. Both the main site and the module use Code First, EF> = 4.3

To create the package, I have one more MVC3 site, and all the functions are inside the scope, so to create the package I just collect libraries, views and all the necessary files. Database migrations work great in a project, and the package is created beautifully.

Now I install the package on the main site through NuGet. This site is in a different solution, and the solution has two projects:

  • MyProject.Web.UI: This is an Mvc3 Project
  • MyProject.EntityFramework: this is a class library with all models, dbContext for MyProject ...

The package is installed correctly, and the areas, areas, and libraries are installed correctly.

Now the problem is how do I update the database? I tried to run "Update-Database" first, but I get a message:

"The migration configuration type 'MyProject.Web.UI was not found in the assembly. (In Visual Studio, you can use the Enable-Migrations command from the package manager console to add migration).

I tried to enable migration using "Enable-Migrations", but I got this different message:

"No context type was found in assembly" MyProject.Web.UI ".

I also tried to start the site and see if the changes are automatically applied, but I get a page with a typical message:

"The model supporting the" NugetPackageDbContext "context has changed since the database was created. Consider using the First Code Migrations to update the database"

I do not know what to do to update the database with the necessary changes in migration, which are included in the NuGet package. Could anyone refresh here on this issue? I'm new to Migrations, maybe there are some configs for updating the database, if there are changes, instead of running commands in the console, I lost a little.

Thank you in advance:)

+4
source share
4 answers

Good news! I think I get it. I was looking for a way to make a NuGet package to upgrade the database to the latest version.

Well, this package comes with an Admin controller, so I added a new action called Update:

public ActionResult Update() { System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<MyPackageDbContext, MyPackage.Migrations.Configuration>()); return View(); } 

In my configuration class for migration, I:

 public Configuration() { AutomaticMigrationsEnabled = true; } 

I have to say that on my way to do all this, I found some strange behaviors. One thing surprises me, I don’t know if this is normal, read the secret:

  • The package is installed with new migrations, but the database is not updated. Therefore, if I access EF, it is affected by this, I get an exception. Good up to that.
  • I go to my action / MyPackage / Admin / Update and run it. It seems to be working. I go to the database and I do not see the changes. Even the migration table does not have a new row.
  • I turn again to the part of EF that previously displayed the exception (point number 1), and then everything goes through, the database is updated, and the migration table shows a new row.

One thing you should notice is that the configuration class is internal, but since it is a module, I needed to access from another assembly. I tried to make it public, but I got some strange warnings / errors that I don’t know if they are related. So in the end I kept it internal, but used

 [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("OtherAssembly")] 

I found some strange behaviors in Visual Studio with all these things of NuGet, CF packages, migrations ... I don’t know if these things were errors, but it all took me two full working days.

I hope this is useful for anyone else who wants to create a CF NuGet package that can be updated.

+2
source

In the package manager console, you will need the default project MyProject.EntityFramework

You may also need to make sure that MyProject.Web.UI installed as a startup project (if there are several), then you can pass the connection string to the update command:

 Update-Database -ConnectionStringName MyConnStringName 

This should update the database correctly if there is no data to be lost.

+1
source

If your DbContext is in MyProject.EntityFramework , then the default project in the package manager console must be set to MyProject.EntityFramework before you can use Update-Database .

0
source

I understand that this question is very old, but since I do not see this answer, I’ll give it up anyway.

To perform migration, etc. on projects or external links, you can still use the same command:

 Enable-Migrations Add-Migration Update-Database 

but you will need to provide additional options. For the Enable-Migrations command, you need to add the name -ContextTypeName and possibly the command -ContextAssemblyName as follows:

 Enable-Migrations -ContextTypeName MyProject.EntityFramework.NugetPackageDbContext -ContextAssemblyName MyProject 

This will give you the migration configuration class in your current project. The other two commands will require you to specify this configuration class:

 Update-Database -ConfigurationTypeName MyProject.Web.UI.Migrations.Configuration 

Hope that helps

0
source

Source: https://habr.com/ru/post/1436643/


All Articles