Search code examples
c#asp.net-mvcvisual-studioasp.net-coremigration

Migration does not create the database


I run normal migration and it normally creates migration, only it does not create the database. On another computer he created usually using the same settings, only on the one that is not giving. only the file in the migration folder. I've tried everything practically, if anyone knows how to answer I'll be grateful. asp.net core MVC and visual studio.

Context class:

  public class ProjectContext : DbContext
  {
  public ProjectContext(DbContextOptions<ProjectContext> options)
  : base(options)

{ }

   (appsettings.json)
   "ConnectionStrings": {
  "ProjetoTIConnection": "Server=localhost;Database=projetoti;Trusted_Connection=true"
}

(Startup.cs)

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<ProjectContext>(options => 
options.UseSqlServer(Configuration.GetConnectionString("ProjetoTIConnection")));

Solution

  • Please check the name of the server where you want to create database.

    In visual studio, you can open View > SQL Server Object Explorer in tool menu, then expend SQL Server node.

    Here is my SQL Server Object Explorer structure:

    enter image description here

    So, the ConnectionStrings in appsettings.json should be :

      "ConnectionStrings": {
        "ProjetoTIConnection": "Server=(localdb)\\mssqllocaldb;Database=projetoti;Trusted_Connection=true"
       } 
    

    Then, after excuting the command of add-migration and update-database, the database named
    projetoti will be created in the server which name is (localdb)\\mssqllocaldb.

    So, please check your server name and ensure that the server name in the connection string is consistent with it.