SQL Server Format of the initialization string does not conform to specification starting at index 0 error EF Core

0s0u357o  于 2023-04-28  发布在  其他
关注(0)|答案(3)|浏览(125)

I am building an application using .NET5, and I am trying to use the EF Core update-migrations and I get the following error:
Format of the initialization string does not conform to specification starting at index 0.

By viewing similar posts, I figured my connectionstring is the problem. However I cant seem to find the correct string to use. If I look at the properties of my localDB it says its connectionstring is:

Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

I set it to be DefaultConnection appsettings.json and connect like this in startup.cs

services.AddDbContext<AppDbContext>(options => options.UseSqlServer("DefaultConnection"));

Any ideas would be greatly appreciated.

Thank you.

EDIT:

Went trough uninstalling SQL Express first, for then to be given this string after installation:

Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;

After installing localDB again I fixed the issue by modifying the connectionstring to:

Server=(localdb)\\MSSQLLocalDB;Database=master;Trusted_Connection=True;
2mbi3lxu

2mbi3lxu1#

.UseSqlServer(string) expects a connection string, not the name of a configuration parameter.

connectionString 
String 
The connection string of the database to connect to.

So you must fetch the connection string from your configuration and pass it to .UseSqlServer . For instance in ASP.NET there's a Startup class with an IConfiguration instance you can use. EG

services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
wz1wpwve

wz1wpwve2#

Sometimes your start project is on multiple projects. be sure just a main project is started. after building the project, you can select some projects to start again. enter image description here

lnlaulya

lnlaulya3#

I've got that error because of some programming mistake I made in the Data/MyAppDbContext.cs

In my case, modifying the code into the following one made that error go away.

namespace MyApp.Data
{
    // DbContext is a session within the DB
    public class MyAppDbContext : DbContext
    {
        public MyAppsDbContext(DbContextOptions<MyAppDbContext> options)
            : base(options)
        {
        }
        
        // Any entity you may use.
        public DbSet<Model> Models { get; set; } = null!;
    }
}

相关问题