© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
20 replies
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();
         }
     }
}
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
{
}
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
}
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)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ WebAPI .NET 7.0 - Encrypt connectionstring in appsettings.json
C#CC# / help
3y ago
❔ How do I use the appsetting.development?
C#CC# / help
3y ago
Newtonsoft.JSON ignore a JsonProperty
C#CC# / help
2y ago
Serialize a JsonDocument/JsonElement with STJ
C#CC# / help
3y ago