change connection string on asp.net core project (appsettings.json)
By : Tudedude
Date : March 29 2020, 07:55 AM
this one helps. You can have environment specific appsettings.json files, for e.g. appsettings.Development.json by calling following in your Startup() method: code :
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
|
.Net Core Cant read connection string from appsettings.json
By : İlknur ilknur ipek
Date : March 29 2020, 07:55 AM
should help you out Your ConnectionStrings section is inside the Logging section, I don't know if that's what you meant but anyway you can access the DatabaseConnection like this: code :
var connection = Configuration["Logging:ConnectionStrings:DatabaseConnection"];
|
.NET Core 2.x get connection string from appsettings.json in dbcontext class
By : Felix
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You should not use Entity Framework like that within ASP.NET Core. You have dependency injection and a properly configured EF context, so you should utilize that. This essentially means: Never create a database context manually using new. Always inject the context as a dependency. Don’t override the OnConfiguring method in the database to configure the context. Configuration is expected to be passed as DbContextOptions, so that the context itself is not responsible of setting up the configuration. Avoid an empty constructor for your database context to avoid misuse where the context stays unconfigured. code :
public class SqliteDbContext : DbContext
{
public SqliteDbContext(DbContextOptions<SqliteDbContext> options) : base(options)
{ }
// define your database sets
public DbSet<Entity> Entities { get; set; }
}
public class CRUDService<T>
{
private readonly SqliteDbContext db;
CRUDService(SqliteDbContext database)
{
db = database;
}
public List<T> Read()
{
return db.Set<T>().ToList();
}
}
|
Cannot connect to a database in .NET Core 3.x (connection string in appsettings.json)
By : JNUPRIYA
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further In my "Models" project (not the main ASP.NET project with the Controllers/Views and appsettings.json) I have the following code generated using dotnet ef.... , My issue was that it no longer seemed to like me doing code :
using (var context = new MyDb())
{
dadeda...
}
_context.dadeda
|
How to pass connection string from appsettings.[name].json to DbContext in .NET Core?
By : Zellcore
Date : March 29 2020, 07:55 AM
I hope this helps you . Everything in .NET Core is built around dependency injection instead of using static instances of things like ConfigurationManager. You could do this in a couple different ways. code :
services.AddDbContext<MyContext>(context => context.UseSqlServer(Configuration.GetConnectionString("name")));
public class SomeClass(MyContext context)
{
}
public class SomeClass(IConfiguration config)
{
config.GetConnectionString("name")
}
|