using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration; using System.Data.Entity.ModelConfiguration.Conventions; using HeuristicLab.Services.Optimization.Billing.Interfaces; using HeuristicLab.Services.Optimization.Billing.Model; namespace HeuristicLab.Services.Optimization.Billing.DataAccess { public class BillingContext : DbContext { 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; } public DbSet ContactInformations { get; set; } public DbSet PaymentInformations { get; set; } public BillingContext() : this("name=BillingContext") { } public BillingContext(string connectionString) : base(connectionString) { Database.SetInitializer(new BillingContextInitiliazer()); // Hook up event to mark existing entities as Unchanged ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, args) => { var entity = args.Entity as IObjectWithState; if (entity != null) { entity.EntityState = State.Unchanged; } }; this.Configuration.LazyLoadingEnabled = false; this.Configuration.ProxyCreationEnabled = false; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new ContactInformationConfiguration()); modelBuilder.Configurations.Add(new PaymentInformationConfiguration()); modelBuilder.Configurations.Add(new ProductConfiguration()); 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.Configurations.Add(new UserConfiguration()); modelBuilder.Conventions.Remove(); base.OnModelCreating(modelBuilder); } #region EntityTypeConfigurations private class ContactInformationConfiguration : EntityTypeConfiguration { internal ContactInformationConfiguration() { this.Ignore(ci => ci.EntityState); } } private class PaymentInformationConfiguration : EntityTypeConfiguration { internal PaymentInformationConfiguration() { this.Ignore(pi => pi.EntityState); } } private class ProductConfiguration : EntityTypeConfiguration { internal ProductConfiguration() { this.Ignore(p => p.EntityState); } } private class OrderConfiguration : EntityTypeConfiguration { internal OrderConfiguration() { this.Ignore(o => o.EntityState); this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId); // TODO: OrderState? } } private class OrderLineConfiguration : EntityTypeConfiguration { internal OrderLineConfiguration() { this.Ignore(ol => ol.EntityState); 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.Ignore(i => i.EntityState); 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.Ignore(i => i.EntityState); 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.Ignore(ur => ur.EntityState); this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId); this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId); } } private class UserConfiguration : EntityTypeConfiguration { internal UserConfiguration() { this.Ignore(u => u.EntityState); this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId); this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId); } } #endregion #region DB Seed Methods // - DropCreateDatabaseAlways // - DropCreateDatabaseIfModelChanges public class BillingContextInitiliazer : DropCreateDatabaseAlways { protected override void Seed(BillingContext context) { Product p1 = new Product() { Name = "OaaS Basic Service Charge", Description = "Create and run your experiments within HeuristicLab Hive", Price = 20.0, ProductType = "Optimization Service" }; Product p2 = new Product() { Name = "Oaas Usage Charges", Description = "Charges for OaaS based on usage data", Price = 0.0, ProductType = "Optimization Service" }; Product p3 = new Product() { Name = "Oaas Support Package", Description = "Charges for OaaS based on usage data", Price = 50.0, ProductType = "Optimization Service" }; p1 = context.Products.Add(p1); p2 = context.Products.Add(p2); p3 = context.Products.Add(p3); User u1 = new Model.User() { Name = "spimming" }; User u2 = new Model.User() { Name = "fschoeppl", PaymentInformation = new List() { new PaymentInformation() { PaymentMethod = PaymentMethod.Visa, CardNumber = "123456789" } } }; u1 = context.Users.Add(u1); u2 = context.Users.Add(u2); ContactInformation ci1 = new ContactInformation() { FirstName = "Max", LastName = "Mustermann", Email = "max.mustermann@fh-hagenberg.at", OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media", Street = "Softwarepark 11", PostalCode = "4232", City = "Hagenberg", }; ci1 = context.ContactInformations.Add(ci1); u1.ContactInformation = ci1; ContactInformation ci2 = new ContactInformation() { FirstName = "Max", LastName = "Mustermann", Email = "max.mustermann@fh-hagenberg.at", OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media", Street = "Softwarepark 11", PostalCode = "4232", City = "Hagenberg", }; ci2 = context.ContactInformations.Add(ci2); u2.ContactInformation = ci2; Order o1 = new Order() { User = u2, State = OrderState.Created, BillingType = BillingType.Post, BillingPeriod = BillingPeriod.Monthly, }; OrderLine ol1 = new OrderLine() { Order = o1, Product = p1, ProductPrice = p1.Price, Quantity = 1 }; o1 = context.Orders.Add(o1); ol1 = context.OrderLines.Add(ol1); context.SaveChanges(); o1.State = OrderState.Active; o1.ActiveSince = DateTime.Now; context.Entry(o1).State = System.Data.EntityState.Modified; context.SaveChanges(); } } #endregion } }