demo20230512/DAL/WeeeDataContext.cs

204 lines
8.6 KiB
C#
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
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<User>
{
public WeeeDataContext()
: base("DefaultConnection")
{
//Database.Log = x => System.Diagnostics.Debug.Write(x);
//Database.SetInitializer<WeeeDataContext>(new WeeeDataContextInitializer());
}
public DbSet<Company> Companies { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<CertificationCompany> CertificationCompanies { get; set; }
public DbSet<NormalCompany> NormalCompanies { get; set; }
public DbSet<OrganizationLCA> OrganizationLCAs { get; set; }
public DbSet<Fab> Fabs { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<LCA> LCAs { get; set; }
public DbSet<ProductLCA> ProductLCAs { get; set; }
public DbSet<ProductLCAReplyRequest> ProductLCAReplyRequests { get; set; }
public DbSet<WorkHour> WorkHours { get; set; }
public DbSet<PowerUsage> PowerUsages { get; set; }
public DbSet<SteamUsage> SteamUsages { get; set; }
public DbSet<WaterUsage> WaterUsages { get; set; }
public DbSet<AbandonedStage> AbandonedStages { get; set; }
public DbSet<SheetHeader> SheetHeaders { get; set; }
public DbSet<Material> Materials { get; set; }
public DbSet<Parameter> Parameters { get; set; }
public DbSet<YearlyParameterType> YearlyParameterTypes { get; set; }
public DbSet<YearlyParameterArea> YearlyParameterAreas { get; set; }
public DbSet<YearlyParameterCategory> YearlyParameterCategories { get; set; }
public DbSet<YearlyParameter> YearlyParameters { get; set; }
public DbSet<NonYearlyParameterCategory> NonYearlyParameterCategories { get; set; }
public DbSet<NonYearlyParameterType> NonYearlyParameterTypes { get; set; }
public DbSet<NonYearlyParameter> NonYearlyParameters { get; set; }
public DbSet<SimaproParameter> SimaproParameters { get; set; }
public DbSet<SimaproParameterType> SimaproTypes { get; set; }
public DbSet<SimaproParameterCategory> SimaproCategories { get; set; }
public DbSet<SimaproVersion> SimaproVersions { get; set; }
public DbSet<PCR> PCRs { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<PublicReferenceFile> PublicReferenceFiles { get; set; }
public DbSet<PublicMessage> PublicMessages { get; set; }
public DbSet<ActionLog> ActionLogs { get; set; }
public DbSet<Refrigerant> LCACommonSurveyForm_Refrigerants { get; set; }//DL-20
public DbSet<Kitchen> Kitchen { get; set; }//DL-45
public DbSet<GasolineEquipment> GasolineEquipment { get; set; }//DL-45
public DbSet<Vehicle> Vehicle { get; set; }//DL-45
public DbSet<FireEquipment> FireEquipment { get; set; }//DL-45
public DbSet<Waste> ProductLCAFabSurveyForm_Wastes { get; set; }//DL-45
public DbSet<LCAStatusLog> LCAStatusLog { get; set; }//DL-51
public DbSet<UserAccountType> UserAccountType { get; set; }//DL-51
public DbSet<OrganizationLCAFabSurveyForm_OtherCompounds> OrganizationOtherCompound { get; set; }//DL-50
public DbSet<UserLCADocExcelTemplate> UserTemplates { get; set; }//CFT-89
public DbSet<DocExcelTemplate> DocExcelTemplates { get; set; }//CFT-89
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(24, 12));
modelBuilder.Entity<YearlyParameter>()
.HasRequired(x => x.Area)
.WithMany(x => x.Parameters)
.WillCascadeOnDelete(true);
modelBuilder.Entity<YearlyParameter>()
.HasRequired(x => x.Type)
.WithMany(x => x.Parameters)
.WillCascadeOnDelete(true);
modelBuilder.Entity<YearlyParameterCategory>()
.HasMany(x => x.Areas)
.WithRequired(y => y.Category)
.WillCascadeOnDelete(false);
modelBuilder.Entity<YearlyParameterCategory>()
.HasMany(x => x.Types)
.WithRequired(y => y.Category)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Material>()
.HasRequired(x=>x.OwnerLCA)
.WithMany(y => y.Materials)
.WillCascadeOnDelete(true);
//modelBuilder.Entity<Material>()
// .HasOptional(x => x.ReplyLCA)
// .WithRequired(y => y.RepliedMaterial)
// .WillCascadeOnDelete(true);
modelBuilder.Entity<Material>()
.HasOptional(x => x.ParentMaterial)
.WithMany(x => x.ChildMaterials)
.HasForeignKey(x => x.ParentMaterialID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<ProductLCAReplyRequest>()
.HasRequired(x => x.SentByWhichMaterial)
.WithOptional(y => y.RequestSent);
modelBuilder.Entity<SnapShotSurveyResult>()
.HasRequired(x => x.SourceLCA)
.WithOptional(y => y.SnapShotSurveyResult);
modelBuilder.Entity<SheetHeader>()
.HasKey(x => new {x.LCAID , x.Category});
modelBuilder.Entity<Refrigerant>()
.HasRequired(x => x.Parameter)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Refrigerant>()
.HasRequired(x => x.Factor)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<AbandonedStage>()
.HasRequired(x => x.OwnerLCA)
.WithOptional(x => x.AbandonedStage);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.IndirectMaterial)
// .WithRequired(y => y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.IndirectMaterial)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.WrapMaterial)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.WorkHourSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.WaterUsageSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.PowerUsageSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.TransportSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.WasteSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.WasteTransportSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<ProductLCA>()
// .HasOptional(x => x.VehicleSheet)
// .WithRequired(y => (ProductLCA)y.LCA);
//modelBuilder.Entity<YearlyParameterArea>()
// .HasMany(x => x.Parameters)
// .WithRequired(y => y.Area)
// .WillCascadeOnDelete(true);
//modelBuilder.Entity<YearlyParameterType>()
// .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();
}
}
}