using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace Gremlin_BlazorServer.Data.DBClasses { public class AccountConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder accountEntity) { accountEntity.HasKey(e => e.AccountId); accountEntity.HasMany(d => d.Contacts).WithOne(p => p.Account).IsRequired(); accountEntity.HasOne(d => d.AccountType).WithMany(p => p.Accounts).IsRequired().OnDelete(DeleteBehavior.Restrict); accountEntity.HasOne(d => d.SubMarket).WithMany(p => p.Accounts).IsRequired().OnDelete(DeleteBehavior.Restrict); accountEntity.HasAlternateKey(e => e.SapAccountNumber); // =Unique accountEntity.Property(e => e.AccountId).ValueGeneratedOnAdd(); accountEntity.Property(e => e.ParentAccountId).HasDefaultValue(0); accountEntity.Property(e => e.AccountName).IsRequired().HasMaxLength(250); accountEntity.Property(e => e.Notes).HasDefaultValue(""); accountEntity.Property(e => e.Street).IsRequired().HasMaxLength(100); accountEntity.Property(e => e.Zip).IsRequired().HasColumnType("Char(5)"); accountEntity.Property(e => e.City).IsRequired().HasMaxLength(50); accountEntity.Property(e => e.FloorOrBuilding).HasMaxLength(50); accountEntity.Property(e => e.Longitude).HasDefaultValue(0); accountEntity.Property(e => e.Latitude).HasDefaultValue(0); accountEntity.Property(e => e.PhoneNumber).IsRequired().HasMaxLength(30); accountEntity.Property(e => e.FaxNumber).HasMaxLength(30).HasDefaultValue(""); accountEntity.Property(e => e.Webpage).HasMaxLength(250).HasDefaultValue(""); accountEntity.Property(e => e.EMail).HasMaxLength(150).HasDefaultValue(""); accountEntity.Property(e => e.SapAccountNumber).IsRequired(); accountEntity.Property(e => e.AccountCreatedInSapOn).IsRequired(); accountEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); accountEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); accountEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); accountEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); accountEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); accountEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); //.HasDefaultValue("Active") //Default-Wert wird nicht gesetzt?!? Bug in EF Core? accountEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true) accountEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class ContactConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder contactEntity) { contactEntity.HasKey(e => e.ContactId); contactEntity.HasOne(p => p.Account).WithMany(d => d.Contacts).IsRequired(); //SW contactEntity.HasMany(q => q.Quotes).WithOne(c => c.Recipient); //entity.HasAlternateKey(e => e.SAPContactNumber); contactEntity.Property(e => e.ContactId); contactEntity.Property(e => e.SapContactNumber).IsRequired(); contactEntity.Property(e => e.AcademicTitle).HasDefaultValue(""); contactEntity.Property(e => e.FirstName).HasDefaultValue(""); contactEntity.Property(e => e.LastName).IsRequired(); contactEntity.Property(e => e.Gender).HasDefaultValue(0); //.IsRequired(true) darf nicht gesetzt werden, da sonst vom DB-Engine NULL nicht erlaubt wird (trotz Definition als Bool? im Code. MySQL kennt kein Bool, sondern wandelt das intern in Tinyint um). contactEntity.Property(e => e.OptInStatus).HasDefaultValue(false); contactEntity.Property(e => e.IsReference).HasDefaultValue(false); contactEntity.Property(e => e.Notes).HasDefaultValue(""); contactEntity.Property(e => e.ValidatedContact).HasDefaultValue(false); contactEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); contactEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); contactEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); contactEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); contactEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); contactEntity.Property(e => e.DataStatus).IsRequired(); //.HasDefaultValue("Active") //Default-Wert wird nicht gesetzt?!? Bug in EF Core? contactEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); //.IsRowVersion() impliziert .ValueGeneratedOnAddOrUpdate() und .IsConcurrencyToken(true) contactEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class QuoteConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder quoteEntity) { quoteEntity.HasKey(e => e.QuoteId); quoteEntity.HasMany(d => d.LineItems).WithOne(p => p.Quote).IsRequired(false).OnDelete(DeleteBehavior.Cascade); quoteEntity.Property(e => e.QuotationNumber).HasColumnType("VARCHAR(255)").IsRequired().ValueGeneratedOnAdd(); quoteEntity.Property(e => e.QuotationDate).IsRequired().ValueGeneratedOnAdd(); quoteEntity.Property(e => e.ValidUntil); quoteEntity.Property(e => e.ValidFor).IsRequired(); //SW quoteEntity.HasOne(e => e.Recipient).WithMany(c => c.Quotes); quoteEntity.Ignore("SalesRep"); quoteEntity.Property(e => e.TotalListprice); quoteEntity.Property(e => e.TotalDiscount); quoteEntity.Property(e => e.TotalNet); quoteEntity.Property(e => e.Vat); quoteEntity.Property(e => e.TotalGross); quoteEntity.Property(e => e.QuoteContains3Pp).HasDefaultValue(false); quoteEntity.Property(e => e.QuoteContainsRb).HasDefaultValue(false); quoteEntity.Property(e => e.QuoteTemplate).HasDefaultValue(""); quoteEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); quoteEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); quoteEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); quoteEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); quoteEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); quoteEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); quoteEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); quoteEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class LineItemConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder entity) { entity.HasKey(e => e.LineItemId); entity.HasOne(p => p.Quote).WithMany(d => d.LineItems).HasForeignKey(fk => fk.QuoteId).IsRequired().OnDelete(DeleteBehavior.Cascade); entity.Property(e => e.Position).IsRequired(); entity.Property(e => e.Amount).IsRequired(); entity.Property(e => e.ProductNumber).IsRequired(); entity.Property(e => e.OptionNumber).HasDefaultValue(""); entity.Property(e => e.SapShortDescription).HasDefaultValue(""); entity.Property(e => e.SapLongDescription).HasDefaultValue(""); entity.Property(e => e.ProductLine).HasDefaultValue(""); entity.Property(e => e.TotalDiscount).IsRequired().HasDefaultValue(0); entity.Property(e => e.SalesDiscount).IsRequired().HasDefaultValue(0); entity.Property(e => e.PromotionalDiscount).IsRequired().HasDefaultValue(0); entity.Property(e => e.ContractualDiscount).IsRequired().HasDefaultValue(0); entity.Property(e => e.DemoDiscount).IsRequired().HasDefaultValue(0); entity.Property(e => e.ListPrice).IsRequired(); entity.Property(e => e.ExtendedListPrice).IsRequired(); entity.Property(e => e.NetPrice).IsRequired(); entity.Property(e => e.Total).IsRequired(); entity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); entity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); entity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); entity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); entity.Property(e => e.DataVersionComment).HasDefaultValue(""); entity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); entity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); entity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class ProductConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder productEntity) { productEntity.HasKey(e => e.ProductId); productEntity.HasOne(d => d.CustomDescription).WithMany(p => p.Products).HasForeignKey("CustomDescriptionId").IsRequired().OnDelete(DeleteBehavior.SetNull); productEntity.HasOne(p => p.ProductLine).WithMany(d => d.Products).HasForeignKey("ProductLineCode").IsRequired().OnDelete(DeleteBehavior.Restrict); productEntity.Property(e => e.CustomDescriptionId).IsRequired(); productEntity.Property(e => e.ProductNumber).IsRequired(); productEntity.Property(e => e.OptionNumber).HasDefaultValue(""); productEntity.Property(e => e.SapShortDescription).HasDefaultValue(""); productEntity.Property(e => e.SapLongDescription).HasDefaultValue(""); productEntity.Property(e => e.Weight).HasDefaultValue(0); productEntity.Property(e => e.ProductStatus).HasDefaultValue("Active"); productEntity.Property(e => e.IntroductionDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); productEntity.Property(e => e.ListPrice).IsRequired(); productEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); productEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); productEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); productEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); productEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); productEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); productEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); productEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class CustomDescriptionConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder customDescriptionEntity) { customDescriptionEntity.HasKey(e => e.CustomDescriptionId); customDescriptionEntity.HasMany(p => p.Products).WithOne(d => d.CustomDescription).IsRequired(false); customDescriptionEntity.HasOne(p => p.Supplier).WithMany(d => d.CustomDescriptions).IsRequired(); customDescriptionEntity.Property(e => e.ProductNumber).IsRequired(); customDescriptionEntity.Property(e => e.OptionNumber).HasDefaultValue(""); customDescriptionEntity.Property(e => e.Heading).IsRequired(); customDescriptionEntity.Property(e => e.DescriptionText).HasDefaultValue(""); customDescriptionEntity.Property(e => e.CoverletterText).HasDefaultValue(""); customDescriptionEntity.Property(e => e.Notes).HasDefaultValue(""); customDescriptionEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); customDescriptionEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); customDescriptionEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); customDescriptionEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); customDescriptionEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); customDescriptionEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); customDescriptionEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); customDescriptionEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class ProductLineConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder productLineEntity) { productLineEntity.HasMany(p => p.Products).WithOne(d => d.ProductLine).IsRequired().OnDelete(DeleteBehavior.Restrict); productLineEntity.Property(e => e.ProductLineDescription).IsRequired(); productLineEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); productLineEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); productLineEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); productLineEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); productLineEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); productLineEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); productLineEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); productLineEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class AccountTypeConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder accountTypeEntity) { //entity.HasKey(e => e.AccountTypeCode); //entity.HasMany(p => p.Accounts).WithOne(d => d.AccountType); //already defined in class Account accountTypeEntity.Property(e => e.AccountTypeCode).IsRequired().HasColumnType("Char(3)"); accountTypeEntity.Property(e => e.AccountTypeDescription).HasColumnType("Varchar(1000)").IsRequired(); accountTypeEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); accountTypeEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); accountTypeEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); accountTypeEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); accountTypeEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); accountTypeEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); accountTypeEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); accountTypeEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class SubMarketConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder subMarketEntity) { //entity.HasKey(e => e.SubMarketCode); //entity.HasMany(p => p.Accounts).WithOne(d => d.SubMarket); //already defined in class Account subMarketEntity.Property(e => e.SubMarketCode).HasColumnType("Char(3)"); subMarketEntity.Property(e => e.SubMarketDescription).HasColumnType("Varchar(1000)").IsRequired(); subMarketEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); subMarketEntity.Property(e => e.DataValidFrom).HasColumnType("DATETIME").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); subMarketEntity.Property(e => e.DataValidUntil).HasColumnType("DATETIME").HasDefaultValueSql("'9999-12-31 23:59:59.000000'").ValueGeneratedOnAdd(); subMarketEntity.Property(e => e.DataVersionNumber).HasDefaultValue(1).IsRequired(); subMarketEntity.Property(e => e.DataVersionComment).HasDefaultValue(""); subMarketEntity.Property(e => e.DataStatus).IsRequired().HasDefaultValue("Active"); subMarketEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); subMarketEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").IsRequired().HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class RegisteredUserConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder registeredUserEntity) { registeredUserEntity.HasKey(e => e.RegisteredUserId); registeredUserEntity.HasMany(d => d.RuSettings).WithOne(p => p.RegisteredUser).IsRequired(); registeredUserEntity.Property(e => e.UserName).IsRequired(); registeredUserEntity.Property(e => e.PasswordHash).IsRequired(); registeredUserEntity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); registeredUserEntity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); registeredUserEntity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } public class RuSettingsConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder rUSettingsEntitity) { rUSettingsEntitity.HasKey(e => e.RuSettingsId); rUSettingsEntitity.HasOne(d => d.RegisteredUser).WithMany(p => p.RuSettings).IsRequired(); rUSettingsEntitity.Property(e => e.SettingKey).IsRequired(); rUSettingsEntitity.Property(e => e.SettingValue).IsRequired(); rUSettingsEntitity.Property(e => e.DataCreationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP").ValueGeneratedOnAdd(); rUSettingsEntitity.Property(e => e.DataModificationDate).HasColumnType("TIMESTAMP").HasDefaultValueSql("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP").ValueGeneratedOnAddOrUpdate().IsConcurrencyToken(); rUSettingsEntitity.Property(e => e.DataModificationByUser).HasColumnType("TINYTEXT").HasDefaultValueSql("ON INSERT CURRENT_USER() ON UPDATE CURRENT_USER()"); } } }