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);
}
}