C
C#2y ago
BekirK

❔ Getting the connectionString from appsetting.json

Hello everyone, in my project I am using a Generic build like this: public class EfGenericRepository<TEntity, TContext> : IGenericRepository<TEntity> where TEntity : class, IEntity, new() where TContext : DbContext, new() { public void Add(TEntity entity) { using (TContext context = new TContext()) { var addedEntity = context.Entry(entity); addedEntity.State = EntityState.Added; context.SaveChanges(); } } } Any EntityDal class is like this: public class EfCategoryDal : EfGenericRepository<Category, SimpraProjectContext>, ICategoryDal { } Context class is like this public class SimpraProjectContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=DESKTOP-xxx; Database=TestOne; Trusted_Connection=true; TrustServerCertificate=True"); } // other codes } I couldn't perform the necessary operations to get the connectionString from appsetting.json. Can you help with this. (I'm using .net 6)
13 Replies
HtmlCompiler
HtmlCompiler2y ago
i simply use the extension method Configuration.GetConnectionString("name")
BekirK
BekirKOP2y ago
Unfortunately I can't simply do this in my project. I also looked at the solution methods on the internet, but I always get an error somewhere. If there are sample codes that I need to implement, I can try.
HtmlCompiler
HtmlCompiler2y ago
what does it mean "i can't"
BekirK
BekirKOP2y ago
public class SimpraProjectContext : DbContext { private readonly IConfiguration _configuration; public SimpraProjectContext(IConfiguration configuration) { _configuration = configuration; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")); } //Other codes } When I do this, Vs isn't build and throws on every EntityDal class that error: Error CS0310 'SimpraProjectContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext' in the generic type or method 'EfGenericRepository<TEntity, TContext>' It means that :/ I don't know what will I do then
Anchy
Anchy2y ago
make a class that you can serialize your appsettings section to, and use the bind method on that section to bind your config to that class and add it to your service collection this needs to be done at the entry point of course where you setup your service container
BekirK
BekirKOP2y ago
I will try to implement what I understand. hope i can make it :/
HtmlCompiler
HtmlCompiler2y ago
i remember i got this error too some time ago
BekirK
BekirKOP2y ago
I got the same error :/ Maybe I couldn't do what you say exactly
Anchy
Anchy2y ago
what is the problem exactly
BekirK
BekirKOP2y ago
Vs isn't build and throws on every EntityDal class that error: Error CS0310 'SimpraProjectContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext' in the generic type or method 'EfGenericRepository<TEntity, TContext>' I got the same error
Anchy
Anchy2y ago
you can configure your db context at your entry-point instead if you prefer you have to construct a DbContextOptions<YourContext> in the constructor and pass it to the base class but when you add your DbContext to the service container you can configure it there
builder.Services.AddDbContext<MyDbContext>(options =>
{
options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"));
});
builder.Services.AddDbContext<MyDbContext>(options =>
{
options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"));
});
public class MyDbContext: DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
}
public class MyDbContext: DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
}
HtmlCompiler
HtmlCompiler2y ago
ah ok so the problems is this where TContext : DbContext, new() your context doesn't satisfy new()
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server