phi
phi
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
logging to txt file :PP
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
ill look into it
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IUserServices, UserServices>();
builder.Services.AddScoped<IUserRepository, UserRepository>();

builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<WebshopDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();

app.Run();
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IUserServices, UserServices>();
builder.Services.AddScoped<IUserRepository, UserRepository>();

builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<WebshopDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();

app.Run();
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
oh you meant lke that haha
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
No description
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
yeah
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
No description
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
ill show you my solution
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
no
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
blazor webassembly
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
im not sure what you mean by this so probably not haha
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
😛
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
yeah.. hes not really much of a teacher its mostly self learning
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
my teacher made it like that haha so i just assumed it was correct
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
so in theory i would need my parameterless constructor in my dbcontext here:
public class WebshopDbContext : DbContext
{
public WebshopDbContext(DbContextOptions<WebshopDbContext> options) : base(options) { }
public WebshopDbContext()
{

}
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<PicturePaths> Paths { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Roles> Roles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=xxxxxx;Initial Catalog=xxxxxxxx;Integrated Security=True");
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.UseLoggerFactory(new ServiceCollection()
.AddLogging(builder => builder.AddConsole()
.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Information))
.BuildServiceProvider().GetService<ILoggerFactory>());
}

base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasOne(c => c.User)
.WithOne(u => u.Customer)
.HasForeignKey<User>(u => u.CustomerId);
modelBuilder.Entity<Customer>().HasKey(c => c.CustomerId);
modelBuilder.Entity<User>().HasKey(u => u.UserId);
}
}
public class WebshopDbContext : DbContext
{
public WebshopDbContext(DbContextOptions<WebshopDbContext> options) : base(options) { }
public WebshopDbContext()
{

}
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<PicturePaths> Paths { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Roles> Roles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=xxxxxx;Initial Catalog=xxxxxxxx;Integrated Security=True");
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.UseLoggerFactory(new ServiceCollection()
.AddLogging(builder => builder.AddConsole()
.AddFilter(DbLoggerCategory.Database.Command.Name, LogLevel.Information))
.BuildServiceProvider().GetService<ILoggerFactory>());
}

base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasOne(c => c.User)
.WithOne(u => u.Customer)
.HasForeignKey<User>(u => u.CustomerId);
modelBuilder.Entity<Customer>().HasKey(c => c.CustomerId);
modelBuilder.Entity<User>().HasKey(u => u.UserId);
}
}
89 replies
CC#
Created by Retro Afro on 4/30/2024 in #help
ASP.NET EF help
if you intend on using Entityframeworkcore you need to have a parameterless constructor if you wanna create migrations as far as im concerned, but if you have an alternative id like to learn from it:)
89 replies