Home > database >  GetConnectionString("DefaultConnection") versus "name=DefaultConnection"
GetConnectionString("DefaultConnection") versus "name=DefaultConnection"

Time:09-13

I noticed some people use

var constring = builder.Configuration.GetConnectionString("DefaultConnection")!;
options.UseSqlServer(constring); 

and some other people just use

options.UseSqlServer("name=DefaultConnection");

Are there any differences?

Note: Both read DefaultConnection key in appsettings.json.

"ConnectionStrings": {
    "DefaultConnection": "server=.;database=MyDb;integrated security=true"
}

CodePudding user response:

No difference rly, the second call

options.UseSqlServer("name=DefaultConnection");

Will try to parse any string found in the connectionStrings config section, with a name of DefaultConnection, they are the same thing, but reading the config explicitly could be a "better" way, as it does not abstract the way that the confiiguration is parsed and set imo

  • Related