❔ 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
i simply use the extension method
Configuration.GetConnectionString("name")
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.
what does it mean "i can't"
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 thenmake 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
I will try to implement what I understand. hope i can make it :/
i remember i got this error too some time ago
I got the same error :/ Maybe I couldn't do what you say exactly
what is the problem exactly
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
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
ah ok so the problems is this
where TContext : DbContext, new()
your context doesn't satisfy new()
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.