tama
tama
CC#
Created by tama on 6/10/2024 in #help
How does EF know what database to use?
im building a function app for azure, so would it make sense to move all my library stuff to the function app project?
19 replies
CC#
Created by tama on 6/10/2024 in #help
How does EF know what database to use?
its built like a console app, however, it's used as a library for another project in my solution if that makes sense
19 replies
CC#
Created by tama on 6/10/2024 in #help
How does EF know what database to use?
at some point, the program.cs code was not out-commented, so i wonder if it saved a copy of the connection string somewhere
19 replies
CC#
Created by tama on 6/10/2024 in #help
How does EF know what database to use?
Program.cs:
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CarManagementSystem.Infrastructure.Data;
using CarManagementSystem.Application.Services;
using CarManagementSystem.Core.Interfaces;
using CarManagementSystem.Infrastructure.Repositories;
using CarManagementSystem.Core.Entities;
using CarManagementSystem.Helpers;

Console.WriteLine(Guid.NewGuid().ToString());


/*var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Register DbContext
services.AddDbContext<CarContext>(options =>
options.UseSqlServer("REAL CONNECTION STRING HOWEVER OUT-COMMENTED"));

// Register repositories and services
services.AddScoped<ICarRepository, CarRepository>();
services.AddScoped<CarService>();
})
.Build();

// Rest of your code remains unchanged
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
var CarService = services.GetRequiredService<CarService>();

// Example usage of the CarService
var Car = new Car
{
SerialNumber = Guid.NewGuid(),
ModelId = "",
ModelName = "",
Manufacturer = "",
PrimaryUser = "",
CarType = CarType.SUV,
Status = CarStatus.Active
};

CarService.AddCar(Car);

var activeCars = CarService.GetActiveCars();
foreach (var d in activeCars)
{
Console.WriteLine($"{d.ModelName} - {d.Status}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

await host.RunAsync();*/
C#
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using CarManagementSystem.Infrastructure.Data;
using CarManagementSystem.Application.Services;
using CarManagementSystem.Core.Interfaces;
using CarManagementSystem.Infrastructure.Repositories;
using CarManagementSystem.Core.Entities;
using CarManagementSystem.Helpers;

Console.WriteLine(Guid.NewGuid().ToString());


/*var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Register DbContext
services.AddDbContext<CarContext>(options =>
options.UseSqlServer("REAL CONNECTION STRING HOWEVER OUT-COMMENTED"));

// Register repositories and services
services.AddScoped<ICarRepository, CarRepository>();
services.AddScoped<CarService>();
})
.Build();

// Rest of your code remains unchanged
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
var CarService = services.GetRequiredService<CarService>();

// Example usage of the CarService
var Car = new Car
{
SerialNumber = Guid.NewGuid(),
ModelId = "",
ModelName = "",
Manufacturer = "",
PrimaryUser = "",
CarType = CarType.SUV,
Status = CarStatus.Active
};

CarService.AddCar(Car);

var activeCars = CarService.GetActiveCars();
foreach (var d in activeCars)
{
Console.WriteLine($"{d.ModelName} - {d.Status}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

await host.RunAsync();*/
19 replies
CC#
Created by tama on 6/10/2024 in #help
How does EF know what database to use?
My DBContext:
C#
public class CarContext : DbContext
{
public CarContext(DbContextOptions<CarContext> options) : base(options)
{
}

public DbSet<Car> Cars { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.Property(d => d.CarType)
.HasConversion<string>();

modelBuilder.Entity<Car>()
.Property(d => d.Status)
.HasConversion<string>();
}
}
C#
public class CarContext : DbContext
{
public CarContext(DbContextOptions<CarContext> options) : base(options)
{
}

public DbSet<Car> Cars { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.Property(d => d.CarType)
.HasConversion<string>();

modelBuilder.Entity<Car>()
.Property(d => d.Status)
.HasConversion<string>();
}
}
19 replies
CC#
Created by tama on 6/8/2024 in #help
Best practice for class constructors?
Is it enough to declare the public variable, and then only include the private variable in the constructor?
8 replies
CC#
Created by tama on 6/8/2024 in #help
Best practice for class constructors?
So, I'm still a little unsure if my example is the correct way to add the public variable. If all variables are public, it's usually enough to declare them and not create an explicit constructor. What about this case?
8 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
Yeah, it's probably not worth the time at that point, since there are still a bunch of features I need to add. I appreciate the help though. 🙂
18 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
I just wonder how... I tried doing something like this but it would not build because it's an obvious error:
C#
Assert.Throws<InvalidOperationException>(() => car.ModelName = "test");
C#
Assert.Throws<InvalidOperationException>(() => car.ModelName = "test");
18 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
Yeah, I am aware it is not the best practice, however, part of the criteria for the project is that the variable cannot be changed after initialization, so it could be nice to be able to point to a unittest and say: "Look, it's not being changed!"
18 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
Just one last thing: Is it possible to showcase that it is not possible to write to the variable after calling the initializer? I'm thinking an xunit test?
18 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
Alright, it seems to work, thanks a lot. 🙂
18 replies
CC#
Created by tama on 6/7/2024 in #help
Entity DB object with immutable variables
Let me just try, maybe you are right. 🙂
18 replies