Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs @ 13757

Last change on this file since 13757 was 9653, checked in by spimming, 12 years ago

#1888:

  • Set flag to indicate a modified model object
  • Add dao query method to specify includes as parameter
  • cleaned up the using of namespaces to qualify types
File size: 8.5 KB
RevLine 
[9602]1using System;
2using System.Collections.Generic;
3using System.Data.Entity;
[9645]4using System.Data.Entity.Infrastructure;
[9577]5using System.Data.Entity.ModelConfiguration;
6using System.Data.Entity.ModelConfiguration.Conventions;
[9645]7using HeuristicLab.Services.Optimization.Billing.Interfaces;
[9577]8using HeuristicLab.Services.Optimization.Billing.Model;
9
10namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
11  public class BillingContext : DbContext {
12    public DbSet<Product> Products { get; set; }
13    public DbSet<User> Users { get; set; }
14    public DbSet<Order> Orders { get; set; }
15    public DbSet<OrderLine> OrderLines { get; set; }
16    public DbSet<Invoice> Invoices { get; set; }
17    public DbSet<InvoiceLine> InvoiceLines { get; set; }
[9579]18    public DbSet<UsageRecord> UsageRecords { get; set; }
[9602]19    public DbSet<ContactInformation> ContactInformations { get; set; }
20    public DbSet<PaymentInformation> PaymentInformations { get; set; }
[9577]21
22    public BillingContext()
[9602]23      : this("name=BillingContext") {
[9577]24
25    }
26
27    public BillingContext(string connectionString)
28      : base(connectionString) {
[9645]29
[9653]30      Database.SetInitializer(new BillingContextInitiliazer());
[9645]31
32      // Hook up event to mark existing entities as Unchanged
33      ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, args) => {
34        var entity = args.Entity as IObjectWithState;
35        if (entity != null) {
36          entity.EntityState = State.Unchanged;
37        }
38      };
39
[9619]40      this.Configuration.LazyLoadingEnabled = false;
41      this.Configuration.ProxyCreationEnabled = false;
[9577]42    }
43
44    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
[9645]45      modelBuilder.Configurations.Add(new ContactInformationConfiguration());
46      modelBuilder.Configurations.Add(new PaymentInformationConfiguration());
47      modelBuilder.Configurations.Add(new ProductConfiguration());
[9577]48      modelBuilder.Configurations.Add(new OrderConfiguration());
49      modelBuilder.Configurations.Add(new OrderLineConfiguration());
50      modelBuilder.Configurations.Add(new InvoiceConfiguration());
51      modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
52      modelBuilder.Configurations.Add(new UsageRecordConfiguration());
[9602]53      modelBuilder.Configurations.Add(new UserConfiguration());
[9577]54
55      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
56
57      base.OnModelCreating(modelBuilder);
58    }
59
60    #region EntityTypeConfigurations
61
[9645]62    private class ContactInformationConfiguration : EntityTypeConfiguration<ContactInformation> {
63      internal ContactInformationConfiguration() {
64        this.Ignore(ci => ci.EntityState);
65      }
66    }
67
68    private class PaymentInformationConfiguration : EntityTypeConfiguration<PaymentInformation> {
69      internal PaymentInformationConfiguration() {
70        this.Ignore(pi => pi.EntityState);
71      }
72    }
73
74    private class ProductConfiguration : EntityTypeConfiguration<Product> {
75      internal ProductConfiguration() {
76        this.Ignore(p => p.EntityState);
77      }
78    }
79
[9577]80    private class OrderConfiguration : EntityTypeConfiguration<Order> {
81      internal OrderConfiguration() {
[9645]82        this.Ignore(o => o.EntityState);
[9577]83        this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId);
84        // TODO: OrderState?
85      }
86    }
87
88    private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> {
89      internal OrderLineConfiguration() {
[9645]90        this.Ignore(ol => ol.EntityState);
[9579]91        this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
[9577]92        this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
93      }
94    }
95
96    private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
97      internal InvoiceConfiguration() {
[9645]98        this.Ignore(i => i.EntityState);
[9577]99        this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
[9579]100        this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
[9577]101      }
102    }
103
104    private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
105      internal InvoiceLineConfiguration() {
[9645]106        this.Ignore(i => i.EntityState);
[9579]107        this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
[9577]108        this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
109      }
110    }
111
112    private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
113      internal UsageRecordConfiguration() {
[9645]114        this.Ignore(ur => ur.EntityState);
[9577]115        this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
[9579]116        this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
[9577]117      }
118    }
119
[9602]120    private class UserConfiguration : EntityTypeConfiguration<User> {
121      internal UserConfiguration() {
[9645]122        this.Ignore(u => u.EntityState);
[9602]123        this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId);
[9641]124        this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId);
[9602]125      }
126    }
127
[9577]128    #endregion
129
130    #region DB Seed Methods
131
[9602]132    // - DropCreateDatabaseAlways<BillingContext>
133    // - DropCreateDatabaseIfModelChanges<BillingContext>
134    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
[9577]135      protected override void Seed(BillingContext context) {
[9602]136        Product p1 = new Product() {
[9641]137          Name = "OaaS Basic Service Charge",
[9577]138          Description = "Create and run your experiments within HeuristicLab Hive",
[9619]139          Price = 20.0,
[9577]140          ProductType = "Optimization Service"
[9602]141        };
[9619]142
143        Product p2 = new Product() {
144          Name = "Oaas Usage Charges",
145          Description = "Charges for OaaS based on usage data",
146          Price = 0.0,
147          ProductType = "Optimization Service"
148        };
149
150        Product p3 = new Product() {
151          Name = "Oaas Support Package",
152          Description = "Charges for OaaS based on usage data",
153          Price = 50.0,
154          ProductType = "Optimization Service"
155        };
156
[9602]157        p1 = context.Products.Add(p1);
[9619]158        p2 = context.Products.Add(p2);
159        p3 = context.Products.Add(p3);
[9602]160
161        User u1 = new Model.User() { Name = "spimming" };
162        User u2 = new Model.User() {
163          Name = "fschoeppl",
164          PaymentInformation = new List<PaymentInformation>() {
165            new PaymentInformation() {
166              PaymentMethod = PaymentMethod.Visa,
167              CardNumber = "123456789" } }
168        };
169
170        u1 = context.Users.Add(u1);
171        u2 = context.Users.Add(u2);
172
173        ContactInformation ci1 = new ContactInformation() {
174          FirstName = "Max",
175          LastName = "Mustermann",
176          Email = "max.mustermann@fh-hagenberg.at",
[9641]177          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
[9602]178          Street = "Softwarepark 11",
179          PostalCode = "4232",
[9641]180          City = "Hagenberg",
[9602]181        };
182
183        ci1 = context.ContactInformations.Add(ci1);
[9641]184        u1.ContactInformation = ci1;
[9602]185
[9641]186        ContactInformation ci2 = new ContactInformation() {
187          FirstName = "Max",
188          LastName = "Mustermann",
189          Email = "max.mustermann@fh-hagenberg.at",
190          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
191          Street = "Softwarepark 11",
192          PostalCode = "4232",
193          City = "Hagenberg",
194        };
195
196        ci2 = context.ContactInformations.Add(ci2);
197        u2.ContactInformation = ci2;
198
[9602]199        Order o1 = new Order() {
200          User = u2,
201          State = OrderState.Created,
202          BillingType = BillingType.Post,
203          BillingPeriod = BillingPeriod.Monthly,
204        };
[9645]205
[9602]206        OrderLine ol1 = new OrderLine() {
207          Order = o1,
208          Product = p1,
209          ProductPrice = p1.Price,
210          Quantity = 1
211        };
[9645]212
[9602]213        o1 = context.Orders.Add(o1);
214        ol1 = context.OrderLines.Add(ol1);
215
[9577]216        context.SaveChanges();
[9602]217
218        o1.State = OrderState.Active;
219        o1.ActiveSince = DateTime.Now;
220        context.Entry(o1).State = System.Data.EntityState.Modified;
221        context.SaveChanges();
[9577]222      }
223    }
224
225    #endregion
226  }
227}
Note: See TracBrowser for help on using the repository browser.