Entity Framework Core 2 Migration issue

303 Views Asked by At

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)

2

There are 2 best solutions below

0
On BEST ANSWER

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;"
0
On

In ASP Net Core there is also a development configuration that gets used. You should notice an icon next to app settings - the "drop down" icon. See if adding your connection string to the development config helps.

Also the way the app settings get accessed has changed so make sure you are writing the correct code to receive the value.