using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using Weee.Models.Paramemter; using Weee.Models; using Microsoft.AspNet.Identity.EntityFramework; using System.Data.Entity.ModelConfiguration.Conventions; namespace Weee.DAL { public class WeeeDataContext : IdentityDbContext { public WeeeDataContext() : base("DefaultConnection") { //Database.Log = x => System.Diagnostics.Debug.Write(x); //Database.SetInitializer(new WeeeDataContextInitializer()); } public DbSet Companies { get; set; } public DbSet Suppliers { get; set; } public DbSet CertificationCompanies { get; set; } public DbSet NormalCompanies { get; set; } public DbSet OrganizationLCAs { get; set; } public DbSet Fabs { get; set; } public DbSet Products { get; set; } public DbSet LCAs { get; set; } public DbSet ProductLCAs { get; set; } public DbSet ProductLCAReplyRequests { get; set; } public DbSet WorkHours { get; set; } public DbSet PowerUsages { get; set; } public DbSet SteamUsages { get; set; } public DbSet WaterUsages { get; set; } public DbSet AbandonedStages { get; set; } public DbSet SheetHeaders { get; set; } public DbSet Materials { get; set; } public DbSet Parameters { get; set; } public DbSet YearlyParameterTypes { get; set; } public DbSet YearlyParameterAreas { get; set; } public DbSet YearlyParameterCategories { get; set; } public DbSet YearlyParameters { get; set; } public DbSet NonYearlyParameterCategories { get; set; } public DbSet NonYearlyParameterTypes { get; set; } public DbSet NonYearlyParameters { get; set; } public DbSet SimaproParameters { get; set; } public DbSet SimaproTypes { get; set; } public DbSet SimaproCategories { get; set; } public DbSet SimaproVersions { get; set; } public DbSet PCRs { get; set; } public DbSet Comments { get; set; } public DbSet PublicReferenceFiles { get; set; } public DbSet PublicMessages { get; set; } public DbSet ActionLogs { get; set; } public DbSet LCACommonSurveyForm_Refrigerants { get; set; }//DL-20 public DbSet Kitchen { get; set; }//DL-45 public DbSet GasolineEquipment { get; set; }//DL-45 public DbSet Vehicle { get; set; }//DL-45 public DbSet FireEquipment { get; set; }//DL-45 public DbSet ProductLCAFabSurveyForm_Wastes { get; set; }//DL-45 public DbSet LCAStatusLog { get; set; }//DL-51 public DbSet UserAccountType { get; set; }//DL-51 public DbSet OrganizationOtherCompound { get; set; }//DL-50 public DbSet UserTemplates { get; set; }//CFT-89 public DbSet DocExcelTemplates { get; set; }//CFT-89 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); modelBuilder.Conventions.Add(new DecimalPropertyConvention(24, 12)); modelBuilder.Entity() .HasRequired(x => x.Area) .WithMany(x => x.Parameters) .WillCascadeOnDelete(true); modelBuilder.Entity() .HasRequired(x => x.Type) .WithMany(x => x.Parameters) .WillCascadeOnDelete(true); modelBuilder.Entity() .HasMany(x => x.Areas) .WithRequired(y => y.Category) .WillCascadeOnDelete(false); modelBuilder.Entity() .HasMany(x => x.Types) .WithRequired(y => y.Category) .WillCascadeOnDelete(false); modelBuilder.Entity() .HasRequired(x=>x.OwnerLCA) .WithMany(y => y.Materials) .WillCascadeOnDelete(true); //modelBuilder.Entity() // .HasOptional(x => x.ReplyLCA) // .WithRequired(y => y.RepliedMaterial) // .WillCascadeOnDelete(true); modelBuilder.Entity() .HasOptional(x => x.ParentMaterial) .WithMany(x => x.ChildMaterials) .HasForeignKey(x => x.ParentMaterialID) .WillCascadeOnDelete(false); modelBuilder.Entity() .HasRequired(x => x.SentByWhichMaterial) .WithOptional(y => y.RequestSent); modelBuilder.Entity() .HasRequired(x => x.SourceLCA) .WithOptional(y => y.SnapShotSurveyResult); modelBuilder.Entity() .HasKey(x => new {x.LCAID , x.Category}); modelBuilder.Entity() .HasRequired(x => x.Parameter) .WithMany() .WillCascadeOnDelete(false); modelBuilder.Entity() .HasRequired(x => x.Factor) .WithMany() .WillCascadeOnDelete(false); modelBuilder.Entity() .HasRequired(x => x.OwnerLCA) .WithOptional(x => x.AbandonedStage); //modelBuilder.Entity() // .HasOptional(x => x.IndirectMaterial) // .WithRequired(y => y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.IndirectMaterial) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.WrapMaterial) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.WorkHourSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.WaterUsageSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.PowerUsageSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.TransportSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.WasteSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.WasteTransportSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasOptional(x => x.VehicleSheet) // .WithRequired(y => (ProductLCA)y.LCA); //modelBuilder.Entity() // .HasMany(x => x.Parameters) // .WithRequired(y => y.Area) // .WillCascadeOnDelete(true); //modelBuilder.Entity() // .HasMany(x => x.Parameters) // .WithRequired(y => y.Type) // .WillCascadeOnDelete(true); base.OnModelCreating(modelBuilder); } public override int SaveChanges() { var changedEntities = ChangeTracker.Entries(); foreach (var changed in changedEntities) { if (changed.Entity is LCA) { var entity = (LCA)changed.Entity; //to do : log the status changes } else if (changed.Entity is NonYearlyParameter) { } } return base.SaveChanges(); } } }