using System.Data.Entity; using System.Data.Entity.ModelConfiguration; using System.Data.Entity.ModelConfiguration.Conventions; using HeuristicLab.Services.Optimization.Billing.Model; namespace HeuristicLab.Services.Optimization.Billing.DataAccess { public class BillingContext : DbContext { public DbSet ContactInformations { get; set; } public DbSet Products { get; set; } public DbSet Users { get; set; } public DbSet Orders { get; set; } public DbSet OrderLines { get; set; } public DbSet Invoices { get; set; } public DbSet InvoiceLines { get; set; } public DbSet UsageRecords { get; set; } // enum PaymentMethod // enum OrderState public BillingContext() : base("name=BillingContext") { } public BillingContext(string connectionString) : base(connectionString) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new OrderConfiguration()); modelBuilder.Configurations.Add(new OrderLineConfiguration()); modelBuilder.Configurations.Add(new InvoiceConfiguration()); modelBuilder.Configurations.Add(new InvoiceLineConfiguration()); modelBuilder.Configurations.Add(new UsageRecordConfiguration()); modelBuilder.Conventions.Remove(); base.OnModelCreating(modelBuilder); } #region EntityTypeConfigurations private class OrderConfiguration : EntityTypeConfiguration { internal OrderConfiguration() { this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId); // TODO: OrderState? } } private class OrderLineConfiguration : EntityTypeConfiguration { internal OrderLineConfiguration() { this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true); this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId); } } private class InvoiceConfiguration : EntityTypeConfiguration { internal InvoiceConfiguration() { this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId); this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId); } } private class InvoiceLineConfiguration : EntityTypeConfiguration { internal InvoiceLineConfiguration() { this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true); this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId); } } private class UsageRecordConfiguration : EntityTypeConfiguration { internal UsageRecordConfiguration() { this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId); this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId); } } #endregion #region DB Seed Methods public class BillingContextInitiliazer : DropCreateDatabaseIfModelChanges { protected override void Seed(BillingContext context) { context.Products.Add(new Model.Product() { Name = "Optimization-as-a-Service - Experiment execution", Description = "Create and run your experiments within HeuristicLab Hive", ProductId = 1, Price = 10.0, ProductType = "Optimization Service" }); context.SaveChanges(); } } #endregion } }