C
C#13mo 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
ffmpeg -i me -f null -
i simply use the extension method Configuration.GetConnectionString("name")
BekirK
BekirK13mo 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.
ffmpeg -i me -f null -
what does it mean "i can't"
BekirK
BekirK13mo 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
Anchy13mo 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
BekirK13mo ago
I will try to implement what I understand. hope i can make it :/
ffmpeg -i me -f null -
i remember i got this error too some time ago
BekirK
BekirK13mo ago
I got the same error :/ Maybe I couldn't do what you say exactly
Anchy
Anchy13mo ago
what is the problem exactly
BekirK
BekirK13mo 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
Anchy13mo 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) { }
}
ffmpeg -i me -f null -
ah ok so the problems is this where TContext : DbContext, new() your context doesn't satisfy new()
Accord
Accord13mo 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
More Posts
❔ C# Powershell, pipe one script to another?Is it possible to run a script and pipe that result to another? The closest I find is something lik❔ Shadowsocks VPN clientHello, i am trying to build a shadowsocks vpn client so im not required to share any information ab[C# Winform with Sql Server] Error: 'Column named StdId cannot be found.Parameter name: columnName'Trying to select a row in Data Grid View in Winform C# and see the image to have an idea to my Table❔ WEB3. Get the the amount of tokenx I can buy with a certain amount of busd.I want to get the amount of tokenx I can buy with different inputs of busd. I have written out some ❔ Understanding reading values from binaryI'm reading a version number from the binary of a client via hex. However, I get different results w✅ Optimizing `HTTPClient.GetAsync()` and large stringsI am writing a client application that consumes a web API at a near real-time rate (100ms). I am usi❔ .NET Service discoveryI have a gRPC server installed on a workstation in an on-premise local network. And I have client apHow do you get the output type from a powershell script with System.Management.Automation?I am trying to figure out how to get the type specified in a powershell script `[OutputType([int])]`❔ Make interface deriving from another interface have a sealed implementation of a function...so that you dont have to write the same code for every child interface. details are in pastebin: ❔ Serialize objects from non-standard formatHello, I am trying to serialize objects from a non-standard format and dont know how to go about it