Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs @ 16147

Last change on this file since 16147 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
Line 
1using System;
2using System.Collections.Generic;
3using System.Data.Entity;
4using System.Data.Entity.Infrastructure;
5using System.Data.Entity.ModelConfiguration;
6using System.Data.Entity.ModelConfiguration.Conventions;
7using HeuristicLab.Services.Optimization.Billing.Interfaces;
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; }
18    public DbSet<UsageRecord> UsageRecords { get; set; }
19    public DbSet<ContactInformation> ContactInformations { get; set; }
20    public DbSet<PaymentInformation> PaymentInformations { get; set; }
21
22    public BillingContext()
23      : this("name=BillingContext") {
24
25    }
26
27    public BillingContext(string connectionString)
28      : base(connectionString) {
29
30      Database.SetInitializer(new BillingContextInitiliazer());
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
40      this.Configuration.LazyLoadingEnabled = false;
41      this.Configuration.ProxyCreationEnabled = false;
42    }
43
44    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
45      modelBuilder.Configurations.Add(new ContactInformationConfiguration());
46      modelBuilder.Configurations.Add(new PaymentInformationConfiguration());
47      modelBuilder.Configurations.Add(new ProductConfiguration());
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());
53      modelBuilder.Configurations.Add(new UserConfiguration());
54
55      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
56
57      base.OnModelCreating(modelBuilder);
58    }
59
60    #region EntityTypeConfigurations
61
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
80    private class OrderConfiguration : EntityTypeConfiguration<Order> {
81      internal OrderConfiguration() {
82        this.Ignore(o => o.EntityState);
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() {
90        this.Ignore(ol => ol.EntityState);
91        this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
92        this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
93      }
94    }
95
96    private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
97      internal InvoiceConfiguration() {
98        this.Ignore(i => i.EntityState);
99        this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
100        this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
101      }
102    }
103
104    private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
105      internal InvoiceLineConfiguration() {
106        this.Ignore(i => i.EntityState);
107        this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
108        this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
109      }
110    }
111
112    private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
113      internal UsageRecordConfiguration() {
114        this.Ignore(ur => ur.EntityState);
115        this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
116        this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
117      }
118    }
119
120    private class UserConfiguration : EntityTypeConfiguration<User> {
121      internal UserConfiguration() {
122        this.Ignore(u => u.EntityState);
123        this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId);
124        this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId);
125      }
126    }
127
128    #endregion
129
130    #region DB Seed Methods
131
132    // - DropCreateDatabaseAlways<BillingContext>
133    // - DropCreateDatabaseIfModelChanges<BillingContext>
134    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
135      protected override void Seed(BillingContext context) {
136        Product p1 = new Product() {
137          Name = "OaaS Basic Service Charge",
138          Description = "Create and run your experiments within HeuristicLab Hive",
139          Price = 20.0,
140          ProductType = "Optimization Service"
141        };
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
157        p1 = context.Products.Add(p1);
158        p2 = context.Products.Add(p2);
159        p3 = context.Products.Add(p3);
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",
177          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
178          Street = "Softwarepark 11",
179          PostalCode = "4232",
180          City = "Hagenberg",
181        };
182
183        ci1 = context.ContactInformations.Add(ci1);
184        u1.ContactInformation = ci1;
185
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
199        Order o1 = new Order() {
200          User = u2,
201          State = OrderState.Created,
202          BillingType = BillingType.Post,
203          BillingPeriod = BillingPeriod.Monthly,
204        };
205
206        OrderLine ol1 = new OrderLine() {
207          Order = o1,
208          Product = p1,
209          ProductPrice = p1.Price,
210          Quantity = 1
211        };
212
213        o1 = context.Orders.Add(o1);
214        ol1 = context.OrderLines.Add(ol1);
215
216        context.SaveChanges();
217
218        o1.State = OrderState.Active;
219        o1.ActiveSince = DateTime.Now;
220        context.Entry(o1).State = System.Data.EntityState.Modified;
221        context.SaveChanges();
222      }
223    }
224
225    #endregion
226  }
227}
Note: See TracBrowser for help on using the repository browser.