Jiry_XD
Jiry_XD
Explore posts from servers
CC#
Created by Jiry_XD on 11/20/2024 in #help
✅ EF Core Junction Tables with seperate columns
In EF Core: I have entity X which has a list of Item Y (This will let EF Core make a junction table to represent the many to many relationship) Now as per my requirements I needed to add a Price to that junction table. So that the price only exists when and if there is a link between X and Y. So I manually made the junction entity XY and added a single item of X and a single item of Y along with ints for their ID's and I added a decimal price. Now my question is: Is this the correct way to add columns to Junction tables?
34 replies
CC#
Created by Jiry_XD on 11/18/2024 in #help
✅ Asp .NET Core production settings.json question.
Hi all, As you know you have an appsettings.json file. Which contains some general things such as:
{
"ConnectionStrings": {
"SqlServer": "afakeconnstring, you should not store it here, overridden with usersecrets"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"EntityFrameworkCore.Triggered": "Warning"
}
},
"AllowedHosts": "*"
}
{
"ConnectionStrings": {
"SqlServer": "afakeconnstring, you should not store it here, overridden with usersecrets"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"EntityFrameworkCore.Triggered": "Warning"
}
},
"AllowedHosts": "*"
}
We also have a launchsettings.json file
{
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
}
}
}
{
"profiles": {
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
}
}
}
In production when we publish our app, what is the default running environment? How do I set the ASPNETCORE_ENVIRONMENT to "Production" in production? How do I set a seperate ConnectionString_SqlServer for production? Does the published production build have these .json files or do they rely solely on the HOST environment variables?
70 replies
CC#
Created by Jiry_XD on 11/18/2024 in #help
✅ Changing ASPNETCORE_ENVIRONMENT causes broken site.
Hi all, I am wanting to test our ASP .NET CORE App coupled with Blazor Wasm in production. So I changed ASPNETCORE_ENVIRONMENT to "Production", and altered Program.cs to work with it:
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();

using (var scope = app.Services.CreateScope())
{ // Require a DbContext from the service provider and seed the database.
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new(dbContext);
await seeder.Seed();
}
}
else
{
using (var scope = app.Services.CreateScope())
{ // Require a DbContext from the service provider and seed the database.
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new(dbContext);
await seeder.SeedProd();
}
}

app.UseHttpsRedirection();

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.MapFallbackToFile("index.html");



app.Run();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();

using (var scope = app.Services.CreateScope())
{ // Require a DbContext from the service provider and seed the database.
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new(dbContext);
await seeder.Seed();
}
}
else
{
using (var scope = app.Services.CreateScope())
{ // Require a DbContext from the service provider and seed the database.
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new(dbContext);
await seeder.SeedProd();
}
}

app.UseHttpsRedirection();

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.MapFallbackToFile("index.html");



app.Run();
Now I only get a white page and it is showing 3 warnings:
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
warn: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[16]
The WebRootPath was not found: MyPathHere Static files may be unavailable.
How can I fix this?
8 replies
CC#
Created by Jiry_XD on 11/1/2024 in #help
Reset identity colums EF Core error.
Hi all, This is my seeding method:
public async Task Seed()
{
dbContext.Database.Migrate();

string dbName = dbContext.Database.GetDbConnection().Database;
if (!dbName.EndsWith("_Tests"))
{
var tableNames = dbContext.Model.GetEntityTypes()
.Select(t => t.GetTableName())
.Distinct()
.ToList();



tableNames.ForEach(tableName =>
{

dbContext.Database.ExecuteSqlRaw($"ALTER TABLE {tableName} NOCHECK CONSTRAINT ALL;");
});

tableNames.ForEach(tableName =>
{
dbContext.Database.ExecuteSqlRaw($"DELETE FROM {tableName};");
dbContext.Database.ExecuteSqlRaw(
$"IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('{tableName}') AND is_identity = 1) " +
$"BEGIN DBCC CHECKIDENT ('{tableName}', RESEED, 0); END");


});



tableNames.ForEach(tableName =>
{
dbContext.Database.ExecuteSqlRaw($"ALTER TABLE {tableName} CHECK CONSTRAINT ALL;");
});

//Seed/insert data
await SeedTable1();
await SeedTable2();
await SeedTable3();

}
}
public async Task Seed()
{
dbContext.Database.Migrate();

string dbName = dbContext.Database.GetDbConnection().Database;
if (!dbName.EndsWith("_Tests"))
{
var tableNames = dbContext.Model.GetEntityTypes()
.Select(t => t.GetTableName())
.Distinct()
.ToList();



tableNames.ForEach(tableName =>
{

dbContext.Database.ExecuteSqlRaw($"ALTER TABLE {tableName} NOCHECK CONSTRAINT ALL;");
});

tableNames.ForEach(tableName =>
{
dbContext.Database.ExecuteSqlRaw($"DELETE FROM {tableName};");
dbContext.Database.ExecuteSqlRaw(
$"IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('{tableName}') AND is_identity = 1) " +
$"BEGIN DBCC CHECKIDENT ('{tableName}', RESEED, 0); END");


});



tableNames.ForEach(tableName =>
{
dbContext.Database.ExecuteSqlRaw($"ALTER TABLE {tableName} CHECK CONSTRAINT ALL;");
});

//Seed/insert data
await SeedTable1();
await SeedTable2();
await SeedTable3();

}
}
17 replies
CC#
Created by Jiry_XD on 10/24/2024 in #help
✅ Implementing IAsyncLifeTime difference
No description
7 replies
CC#
Created by Jiry_XD on 10/24/2024 in #help
✅ Integration testing
No description
24 replies
CC#
Created by Jiry_XD on 10/21/2024 in #help
How to prevent nulls using this approach for Object Initializers?
In C# I have this class:
using System.Diagnostics.CodeAnalysis;
using System.Net.Mail;

namespace Domain.Customers;

public class EmailAddress
{
private MailAddress email;

public EmailAddress()
{
// Default constructor so I can use object initializer.
}

[SetsRequiredMembers]
public EmailAddress(string email)
{
Email = email;
}
public required string Email
{
get => email.ToString();
set => email = new MailAddress(value);
}

}
using System.Diagnostics.CodeAnalysis;
using System.Net.Mail;

namespace Domain.Customers;

public class EmailAddress
{
private MailAddress email;

public EmailAddress()
{
// Default constructor so I can use object initializer.
}

[SetsRequiredMembers]
public EmailAddress(string email)
{
Email = email;
}
public required string Email
{
get => email.ToString();
set => email = new MailAddress(value);
}

}
As you can see I have two constructors, when using object initializers I need to use one of them. I like using object initializers using no constructor aka the default constructor but because I added it there is no default so I added it. But now the problem is that people can call new EmailAddress() without passing an email and it can stay null that way? How can I still be able to have an empty constructor so I can use object initializers without having the risk somoene calls new EmailAdress();?
20 replies
CC#
Created by Jiry_XD on 10/21/2024 in #help
Test setters indirectly via constructor or test them directly?
Title 🙂
32 replies
CC#
Created by Jiry_XD on 9/30/2024 in #help
✅ What's new in .NET 8 regarding making a WASM project?
Hello guys, I am looking into learning Blazor WASM. I was viewing some tutorials online on how to setup the project but everything seems to have changed since .NET 8. Normally you could make a Blazor WASM project + ASP .NET Core hosted, now you have to do it manually? Or is there still an option to do it automatically? Why did they remove it, is it not the standard anymore? If so what is the new standard?
46 replies