[9577] | 1 | using System.Data.Entity;
|
---|
| 2 | using System.Data.Entity.ModelConfiguration;
|
---|
| 3 | using System.Data.Entity.ModelConfiguration.Conventions;
|
---|
| 4 | using HeuristicLab.Services.Optimization.Billing.Model;
|
---|
| 5 |
|
---|
| 6 | namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
|
---|
| 7 | public class BillingContext : DbContext {
|
---|
| 8 | public DbSet<ContactInformation> ContactInformations { get; set; }
|
---|
| 9 | public DbSet<Product> Products { get; set; }
|
---|
| 10 | public DbSet<User> Users { get; set; }
|
---|
| 11 | public DbSet<Order> Orders { get; set; }
|
---|
| 12 | public DbSet<OrderLine> OrderLines { get; set; }
|
---|
| 13 | public DbSet<Invoice> Invoices { get; set; }
|
---|
| 14 | public DbSet<InvoiceLine> InvoiceLines { get; set; }
|
---|
[9579] | 15 | public DbSet<UsageRecord> UsageRecords { get; set; }
|
---|
[9577] | 16 |
|
---|
| 17 | // enum PaymentMethod
|
---|
| 18 | // enum OrderState
|
---|
| 19 |
|
---|
| 20 | public BillingContext()
|
---|
| 21 | : base("name=BillingContext") {
|
---|
| 22 |
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | public BillingContext(string connectionString)
|
---|
| 26 | : base(connectionString) {
|
---|
| 27 |
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | protected override void OnModelCreating(DbModelBuilder modelBuilder) {
|
---|
| 31 | modelBuilder.Configurations.Add(new OrderConfiguration());
|
---|
| 32 | modelBuilder.Configurations.Add(new OrderLineConfiguration());
|
---|
| 33 | modelBuilder.Configurations.Add(new InvoiceConfiguration());
|
---|
| 34 | modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
|
---|
| 35 | modelBuilder.Configurations.Add(new UsageRecordConfiguration());
|
---|
| 36 |
|
---|
| 37 | modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
|
---|
| 38 |
|
---|
| 39 | base.OnModelCreating(modelBuilder);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | #region EntityTypeConfigurations
|
---|
| 43 |
|
---|
| 44 | private class OrderConfiguration : EntityTypeConfiguration<Order> {
|
---|
| 45 | internal OrderConfiguration() {
|
---|
| 46 | this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId);
|
---|
| 47 | // TODO: OrderState?
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> {
|
---|
| 52 | internal OrderLineConfiguration() {
|
---|
[9579] | 53 | this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
|
---|
[9577] | 54 | this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
|
---|
| 59 | internal InvoiceConfiguration() {
|
---|
| 60 | this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
|
---|
[9579] | 61 | this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
|
---|
[9577] | 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
|
---|
| 66 | internal InvoiceLineConfiguration() {
|
---|
[9579] | 67 | this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
|
---|
[9577] | 68 | this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
|
---|
| 73 | internal UsageRecordConfiguration() {
|
---|
| 74 | this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
|
---|
[9579] | 75 | this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
|
---|
[9577] | 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
| 81 | #region DB Seed Methods
|
---|
| 82 |
|
---|
| 83 | public class BillingContextInitiliazer : DropCreateDatabaseIfModelChanges<BillingContext> {
|
---|
| 84 | protected override void Seed(BillingContext context) {
|
---|
| 85 | context.Products.Add(new Model.Product() {
|
---|
| 86 | Name = "Optimization-as-a-Service - Experiment execution",
|
---|
| 87 | Description = "Create and run your experiments within HeuristicLab Hive",
|
---|
| 88 | ProductId = 1,
|
---|
| 89 | Price = 10.0,
|
---|
| 90 | ProductType = "Optimization Service"
|
---|
| 91 | });
|
---|
| 92 | context.SaveChanges();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #endregion
|
---|
| 97 | }
|
---|
| 98 | }
|
---|