Search code examples
c#entity-framework-coreasp.net-core-2.0entity-framework-core-migrations

Entity Framework Core 2 Migration issue


I am building app using ASP.NET Core 2 and DB part is EF core 2.

i have model like this

public class User        
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }   
}

This is context file

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

    }

    public DbSet<ChatMessage> ChatMessages { get; set; }
}

and finally startup.cs

services.AddDbContext<EFCoreDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MooltiRoomDatabase")));

And connection string in appsettings.json

"ConnectionStrings": {
    "MooltiRoomDatabase": "Server=DESKTOP-BCQ6IAU\\CHATDB,Database=MultiRoomChat;Trusted_Connection=True;"
  }

Through package manager console i was runing Add-Migration the command completed successfully despite the fact that in DB it did not create corresponding table,after that i've read that i need to run Update-Database command for creating table and when i run this command, i got a error like this

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)


Solution

  • As you can see in the error message, 'Connection string is not valid.' You used "," instead of ";" before the Database.

    So now you have:

    "Server=DESKTOP-BCQ6IAU\\CHATDB,Database=MultiRoomChat;Trusted_Connection=True;"
    

    But, you should use this:

    "Server=DESKTOP-BCQ6IAU\\CHATDB;Database=MultiRoomChat;Trusted_Connection=True;"