Rowin ツ
Rowin ツ
Explore posts from servers
CC#
Created by Rowin ツ on 6/19/2024 in #help
Database suffix builder
Question, is doing something like this acceptable?
public class EditorDbContext : DbContext
{
private readonly string _tableSuffix;

public DbSet<EditorItem> EditorItems { get; set; }

public EditorDbContext(DbContextOptions<EditorDbContext> options, string tableSuffix)
: base(options)
{
_tableSuffix = tableSuffix ?? throw new ArgumentNullException(nameof(tableSuffix));
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new EditorItemConfiguration(_tableSuffix));
}
}
public class EditorDbContext : DbContext
{
private readonly string _tableSuffix;

public DbSet<EditorItem> EditorItems { get; set; }

public EditorDbContext(DbContextOptions<EditorDbContext> options, string tableSuffix)
: base(options)
{
_tableSuffix = tableSuffix ?? throw new ArgumentNullException(nameof(tableSuffix));
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new EditorItemConfiguration(_tableSuffix));
}
}
public class EditorItemConfiguration : IEntityTypeConfiguration<EditorItem>
{
private string _suffix;
public EditorItemConfiguration(string suffix)
{
_suffix = suffix;
}
public void Configure(EntityTypeBuilder<EditorItem> builder)
{
builder.ToTable($"tblplantekstobjects{_suffix}");
}
}
public class EditorItemConfiguration : IEntityTypeConfiguration<EditorItem>
{
private string _suffix;
public EditorItemConfiguration(string suffix)
{
_suffix = suffix;
}
public void Configure(EntityTypeBuilder<EditorItem> builder)
{
builder.ToTable($"tblplantekstobjects{_suffix}");
}
}
Though I think is is right I'm still getting some dependency injection problems but im not sure if i should spend time on fixing those or if there is a different way For example this:
public async Task<List<EditorItemDto>> GetEditorTree(int regelingId)
{
string tableSuffix = "3";
using (var dbContext = _dbContextFactory.CreateDbContext(tableSuffix))
{
var items = await dbContext.EditorItems.ToListAsync();
Console.WriteLine($"Entities from EditorItem_{tableSuffix}: {items.Count}");
var list = _mapper.Map<List<EditorItemDto>>(items);
return list;
}
}
public async Task<List<EditorItemDto>> GetEditorTree(int regelingId)
{
string tableSuffix = "3";
using (var dbContext = _dbContextFactory.CreateDbContext(tableSuffix))
{
var items = await dbContext.EditorItems.ToListAsync();
Console.WriteLine($"Entities from EditorItem_{tableSuffix}: {items.Count}");
var list = _mapper.Map<List<EditorItemDto>>(items);
return list;
}
}
33 replies
CC#
Created by Rowin ツ on 6/7/2024 in #help
Docker container not port forwarding
Hello, yesterday I was in here as well regarding some problems with my port forwarding. Im trying to get my docker container to run on localhost:8080 and also make use of some certificates because I want to use another local project to test out the endpoints on my api which I am trying to deploy. I have the following code Dockerfile:
# Stage 1: Base image with HTTPS setup
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ENV HOME=/root

USER root
WORKDIR /app
ENV DOTNET_URLS=https://+:8080
EXPOSE 8080

# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app


# Stage 2: Build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["authapi.csproj", "."]
RUN dotnet restore "./authapi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Stage 3: Publish the application
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage 4: Final stage to run the application
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "authapi.dll"]
# Stage 1: Base image with HTTPS setup
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ENV HOME=/root

USER root
WORKDIR /app
ENV DOTNET_URLS=https://+:8080
EXPOSE 8080

# Ensure the /app/certs directory exists and copy the HTTPS certificate
RUN mkdir -p /app/certs
COPY "Certs/aspnetapp.pfx" "/app/certs/aspnetapp.pfx"
RUN chown -R app:app /app/certs
USER app


# Stage 2: Build the application
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["authapi.csproj", "."]
RUN dotnet restore "./authapi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Stage 3: Publish the application
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./authapi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage 4: Final stage to run the application
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "authapi.dll"]
Docker-compose:
services:
authapi:
image: authapi:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
- authapi-network
volumes:
- /certs:/Certs

networks:
authapi-network:
driver: bridge
services:
authapi:
image: authapi:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
- authapi-network
volumes:
- /certs:/Certs

networks:
authapi-network:
driver: bridge
Appsettings:
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "./certs/aspnetapp.pfx",
"Password": "<My valid password is here>"
}
}
}
},
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:8080",
"Certificate": {
"Path": "./certs/aspnetapp.pfx",
"Password": "<My valid password is here>"
}
}
}
},
Launchsettings:
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/public",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:8080"
},
"certificates": {
"Default": {
"path": "Certs/aspnetapp.pfx",
"password": "<My valid password>"
}
},
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/public",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:8080"
},
"certificates": {
"Default": {
"path": "Certs/aspnetapp.pfx",
"password": "<My valid password>"
}
},
82 replies
CC#
Created by Rowin ツ on 5/22/2024 in #help
Create a DbContext at build time, but change the connectionstring at runtime.
Hello, We are currently refactoring a few of our projects, and one of the problems we are having is wanting to switch databases in an SQL server. For example, we have our SQL server localhost with Database Test1, Test2, Test3, etc.... and we would like to change to which database the api is calling based on some other factors which is given by info from Redis. Now we can't find anything quite solid regarding achieving this without having to rebuild the whole DbContext. To keep in mind, all those databases are copies of eachother. But theyre are just meant for different people. Hence we think it would work by not having to re-create the DbContext.
8 replies
CC#
Created by Rowin ツ on 3/25/2024 in #help
EF Not finding columns
I'm trying to create an API for my database using the IdentityManager, when simply wanting to grab all my users from the database using my controller I get this error: "message": "Invalid column name 'ConcurrencyStamp'.\r\nInvalid column name 'LockoutEnd'.\r\nInvalid column name 'NormalizedEmail'.\r\nInvalid column name 'NormalizedUserName'." Ive checked the database and its columns and those columns are present. Im not really sure where else I should look. Ive applied the correct table name to the modelBuilder.
29 replies
CC#
Created by Rowin ツ on 2/27/2024 in #help
Unit testing FakeItEasy
No description
8 replies