public CodeOutput GenerateFakeDbSet() { var filename = "FakeDbSet" + Settings.FileExtension; if (!CanWriteFakeContext()) { FileManagementService.DeleteFile(filename); return(null); } var data = new FakeDbSetModel { DbContextClassModifiers = Settings.DbContextClassModifiers, DbContextClassIsPartial = Settings.DbContextClassIsPartial(), IsEfCore2 = Settings.IsEfCore2(), IsEfCore3 = Settings.IsEfCore3(), IsEfCore5 = Settings.IsEfCore5(), IsEfCore3Plus = Settings.IsEfCore3Plus() }; var co = new CodeOutput(string.Empty, filename, "Fake DbSet", GlobalUsings); co.AddUsings(Template.FakeDbSetUsings(data)); co.AddCode(Template.Transform(Template.FakeDbSet(), data)); return(co); }
public CodeOutput GenerateFakeContext() { var filename = "Fake" + Settings.DbContextName + Settings.FileExtension; if (!CanWriteFakeContext()) { FileManagementService.DeleteFile(filename); return(null); } var data = new FakeContextModel { DbContextClassModifiers = Settings.DbContextClassModifiers, DbContextName = Settings.DbContextName, DbContextBaseClass = Settings.DbContextBaseClass, contextInterface = string.IsNullOrWhiteSpace(Settings.DbContextInterfaceName) ? "" : " : " + Settings.DbContextInterfaceName, DbContextClassIsPartial = Settings.DbContextClassIsPartial(), tables = _tables, storedProcs = _storedProcs, hasStoredProcs = _hasStoredProcs, tableValuedFunctions = _tableValuedFunctions, scalarValuedFunctions = _scalarValuedFunctions, hasTableValuedFunctions = _hasTableValuedFunctions && _filter.IncludeTableValuedFunctions, hasScalarValuedFunctions = _hasScalarValuedFunctions && _filter.IncludeScalarValuedFunctions, }; var co = new CodeOutput(string.Empty, filename, "Fake Database context", _globalUsings); co.AddUsings(_template.FakeDatabaseContextUsings(data, _filter)); co.AddCode(Template.Transform(_template.FakeDatabaseContext(), data)); return(co); }
public CodeOutput GenerateInterface() { var filename = Settings.DbContextInterfaceName + Settings.FileExtension; if (!CanWriteInterface()) { FileManagementService.DeleteFile(filename); return(null); } var data = new InterfaceModel { interfaceModifier = Settings.DbContextInterfaceModifiers ?? "public partial", DbContextInterfaceName = Settings.DbContextInterfaceName, DbContextInterfaceBaseClasses = Settings.DbContextInterfaceBaseClasses, DbContextName = Settings.DbContextName, tables = _tables.Where(x => x.DbSetModifier == "public").ToList(), AdditionalContextInterfaceItems = Settings.AdditionalContextInterfaceItems.Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList(), addSaveChanges = !Settings.UseInheritedBaseInterfaceFunctions, storedProcs = _storedProcs, hasStoredProcs = _hasStoredProcs, tableValuedFunctions = _tableValuedFunctions, scalarValuedFunctions = _scalarValuedFunctions, hasTableValuedFunctions = _hasTableValuedFunctions && _filter.IncludeTableValuedFunctions, hasScalarValuedFunctions = _hasScalarValuedFunctions && _filter.IncludeScalarValuedFunctions, }; var co = new CodeOutput(string.Empty, filename, "Database context interface", _globalUsings); co.AddUsings(_template.DatabaseContextInterfaceUsings(data)); co.AddCode(Template.Transform(_template.DatabaseContextInterface(), data)); return(co); }
public void Add(string key, CodeOutput code) { if (code != null) { Files.Add(key, code); } }
public CodeOutput GenerateStoredProcReturnModel(StoredProcedure sp) { if (!CanWriteStoredProcReturnModel()) { return(null); } var multipleModelReturnColumns = new List <MultipleModelReturnColumns>(); var model = 0; foreach (var returnModel in sp.ReturnModels) { multipleModelReturnColumns.Add(new MultipleModelReturnColumns(++model, returnModel.Select(sp.WriteStoredProcReturnColumn).ToList())); } var data = new StoredProcReturnModel { ResultClassModifiers = Settings.ResultClassModifiers, WriteStoredProcReturnModelName = sp.WriteStoredProcReturnModelName(_filter), SingleModel = sp.ReturnModels.Count == 1, SingleModelReturnColumns = sp.ReturnModels .First() .Select(sp.WriteStoredProcReturnColumn) .ToList(), MultipleModelReturnColumns = multipleModelReturnColumns }; var co = new CodeOutput(sp.WriteStoredProcReturnModelName(_filter) + Settings.FileExtension, null, GlobalUsings); co.AddUsings(Template.StoredProcReturnModelUsings()); co.AddCode(Template.Transform(Template.StoredProcReturnModels(), data)); return(co); }
public CodeOutput GeneratePocoConfiguration(Table table, Generator generator) { if (!CanWritePocoConfiguration()) { return(null); } var columns = table.Columns .Where(x => !x.Hidden && !string.IsNullOrEmpty(x.Config)) .OrderBy(x => x.Ordinal) .ToList(); var foreignKeys = columns.SelectMany(x => x.ConfigFk).OrderBy(o => o).ToList(); var indexes = _generator.IndexModelBuilder(table); var hasIndexes = indexes != null && indexes.Any(); var data = new PocoConfigurationModel { Name = table.DbName, ConfigurationClassName = table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName, NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(), Schema = table.Schema.DbName, PrimaryKeyNameHumanCase = table.PrimaryKeyNameHumanCase(), HasSchema = !string.IsNullOrEmpty(table.Schema.DbName), ClassModifier = Settings.EntityClassesModifiers, ClassComment = table.WriteComments(), Columns = columns.Select(x => x.Config).ToList(), HasReverseNavigation = table.ReverseNavigationProperty.Count > 0, ReverseNavigationProperty = table.ReverseNavigationProperty .OrderBy(x => x.Definition) .Select(x => new PocoReverseNavigationPropertyModel { ReverseNavHasComment = Settings.IncludeComments != CommentsStyle.None && !string.IsNullOrEmpty(x.Comments), ReverseNavComment = Settings.IncludeComments != CommentsStyle.None ? x.Comments : string.Empty, AdditionalReverseNavigationsDataAnnotations = Settings.AdditionalReverseNavigationsDataAnnotations, AdditionalDataAnnotations = x.AdditionalDataAnnotations, Definition = x.Definition }) .ToList(), HasForeignKey = foreignKeys.Any(), ForeignKeys = foreignKeys, MappingConfiguration = table.MappingConfiguration, ConfigurationClassesArePartial = Settings.ConfigurationClassesArePartial(), Indexes = indexes, HasIndexes = hasIndexes }; var co = new CodeOutput(table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName + Settings.FileExtension, null, GlobalUsings); co.AddUsings(Template.PocoConfigurationUsings(data)); co.AddCode(Template.Transform(Template.PocoConfiguration(), data)); return(co); }
public CodeOutput GenerateEnum(Enumeration enumeration) { if (!CanWriteEnums()) { return(null); } var co = new CodeOutput(enumeration.EnumName + Settings.FileExtension, null, null); co.AddCode(Template.Transform(Template.Enums(), enumeration)); return(co); }
public void Add(string key, CodeOutput code) { if (code != null) { if (Files.ContainsKey(key)) { var error = string.Format("{0} already exists in the code output list. {1} and {2} both resolve to the same C# name. Filter one of them out.", key, Files[key].DbName, code.DbName); throw new Exception(error); } Files.Add(key, code); } }
public CodeOutput GenerateEnum(Enumeration enumeration) { var filename = enumeration.EnumName + Settings.FileExtension; if (!CanWriteEnums()) { FileManagementService.DeleteFile(filename); return(null); } var co = new CodeOutput(enumeration.EnumName, filename, null, null); co.AddUsings(_template.EnumUsings()); co.AddCode(Template.Transform(_template.Enums(), enumeration)); return(co); }
public CodeOutput GenerateFactory() { if (!CanWriteFactory()) { return(null); } var data = new FactoryModel { classModifier = Settings.DbContextClassModifiers, contextName = Settings.DbContextName }; var co = new CodeOutput(Settings.DbContextName + "Factory" + Settings.FileExtension, "Database context factory", GlobalUsings); co.AddUsings(Template.DatabaseContextFactoryUsings(data)); co.AddCode(Template.Transform(Template.DatabaseContextFactory(), data)); return(co); }
public CodeOutput GenerateFactory() { var filename = Settings.DbContextName + "Factory" + Settings.FileExtension; if (!CanWriteFactory()) { FileManagementService.DeleteFile(filename); return(null); } var data = new FactoryModel { classModifier = Settings.DbContextClassModifiers, contextName = Settings.DbContextName }; var co = new CodeOutput(string.Empty, filename, "Database context factory", _globalUsings); co.AddUsings(_template.DatabaseContextFactoryUsings(data)); co.AddCode(Template.Transform(_template.DatabaseContextFactory(), data)); return(co); }
public CodeOutput GenerateFakeDbSet() { if (!CanWriteFakeContext()) { return(null); } var data = new FakeDbSetModel { DbContextClassModifiers = Settings.DbContextClassModifiers, DbContextClassIsPartial = Settings.DbContextClassIsPartial(), IsEfCore2 = Settings.TemplateType == TemplateType.EfCore2, IsEfCore3 = Settings.TemplateType == TemplateType.EfCore3 }; var co = new CodeOutput("FakeDbSet" + Settings.FileExtension, "Fake DbSet", GlobalUsings); co.AddUsings(Template.FakeDbSetUsings(data)); co.AddCode(Template.Transform(Template.FakeDbSet(), data)); return(co); }
public CodeOutput GeneratePoco(Table table) { var filename = table.NameHumanCaseWithSuffix() + Settings.FileExtension; if (!CanWritePoco()) { FileManagementService.DeleteFile(filename); return(null); } var isEfCore3Plus = Settings.IsEfCore3Plus(); var data = new PocoModel { UseHasNoKey = isEfCore3Plus && table.IsView && !table.HasPrimaryKey, HasNoPrimaryKey = !table.HasPrimaryKey, Name = table.DbName, NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(), ClassModifier = Settings.EntityClassesModifiers, ClassComment = table.WriteComments(), ExtendedComments = table.WriteExtendedComments(), ClassAttributes = table.WriteClassAttributes(), BaseClasses = table.BaseClasses, InsideClassBody = Settings.WriteInsideClassBody(table), Columns = table.Columns .Where(x => !x.Hidden && !x.ExistsInBaseClass) .OrderBy(x => x.Ordinal) .Select((col, index) => new PocoColumnModel { AddNewLineBefore = index > 0 && (((Settings.IncludeExtendedPropertyComments == CommentsStyle.InSummaryBlock || Settings.IncludeComments == CommentsStyle.InSummaryBlock) && !string.IsNullOrEmpty(col.SummaryComments)) || (col.Attributes != null && col.Attributes.Any())), HasSummaryComments = (Settings.IncludeExtendedPropertyComments == CommentsStyle.InSummaryBlock || Settings.IncludeComments == CommentsStyle.InSummaryBlock) && !string.IsNullOrEmpty(col.SummaryComments), SummaryComments = !string.IsNullOrEmpty(col.SummaryComments) ? SecurityElement.Escape(col.SummaryComments) : null, Attributes = col.Attributes, OverrideModifier = col.OverrideModifier, WrapIfNullable = col.WrapIfNullable(), NameHumanCase = col.NameHumanCase, PrivateSetterForComputedColumns = Settings.UsePrivateSetterForComputedColumns && col.IsComputed ? "private " : string.Empty, PropertyInitialisers = Settings.UsePropertyInitialisers ? (string.IsNullOrWhiteSpace(col.Default) ? string.Empty : string.Format(" = {0};", col.Default)) : string.Empty, InlineComments = col.InlineComments }) .ToList(), HasReverseNavigation = table.ReverseNavigationProperty.Count > 0, ReverseNavigationProperty = table.ReverseNavigationProperty .OrderBy(x => x.Definition) .Select(x => new PocoReverseNavigationPropertyModel { ReverseNavHasComment = Settings.IncludeComments != CommentsStyle.None && !string.IsNullOrEmpty(x.Comments), ReverseNavComment = Settings.IncludeComments != CommentsStyle.None ? x.Comments : string.Empty, AdditionalReverseNavigationsDataAnnotations = Settings.AdditionalReverseNavigationsDataAnnotations, AdditionalDataAnnotations = x.AdditionalDataAnnotations, Definition = x.Definition }) .ToList(), HasForeignKey = table.HasForeignKey, ForeignKeyTitleComment = Settings.IncludeComments != CommentsStyle.None && table.Columns.SelectMany(x => x.EntityFk).Any() ? " // Foreign keys" + Environment.NewLine : string.Empty, ForeignKeys = table.Columns .SelectMany(x => x.EntityFk) .OrderBy(o => o.Definition) .Select(x => new PocoForeignKeyModel { HasFkComment = Settings.IncludeComments != CommentsStyle.None && !string.IsNullOrEmpty(x.Comments), FkComment = x.Comments, AdditionalForeignKeysDataAnnotations = Settings.AdditionalForeignKeysDataAnnotations, AdditionalDataAnnotations = x.AdditionalDataAnnotations, Definition = x.Definition }) .ToList(), CreateConstructor = !Settings.UsePropertyInitialisers && ( table.Columns.Any(c => c.Default != string.Empty && !c.Hidden) || table.ReverseNavigationCtor.Any() || Settings.EntityClassesArePartial() ), ColumnsWithDefaults = table.Columns .Where(c => c.Default != string.Empty && !c.Hidden && Settings.IncludeColumnsWithDefaults) .OrderBy(x => x.Ordinal) .Select(x => new PocoColumnsWithDefaultsModel { NameHumanCase = x.NameHumanCase, Default = x.Default }) .ToList(), ReverseNavigationCtor = table.ReverseNavigationCtor, EntityClassesArePartial = Settings.EntityClassesArePartial() }; var co = new CodeOutput(table.DbName, filename, null, _globalUsings); co.AddUsings(_template.PocoUsings(data)); co.AddCode(Template.Transform(_template.Poco(), data)); return(co); }
public CodeOutput GenerateContext() { var filename = Settings.DbContextName + Settings.FileExtension; if (!CanWriteContext()) { FileManagementService.DeleteFile(filename); return(null); } var indexes = new List <string>(); var hasSpatialTypes = false; foreach (var table in _tables) { var columns = table.Table.Columns .Where(x => !x.Hidden && !string.IsNullOrEmpty(x.Config)) .OrderBy(x => x.Ordinal) .ToList(); if (!Settings.DisableGeographyTypes && !hasSpatialTypes) { hasSpatialTypes = columns.Any(x => x.IsSpatial); } indexes.AddRange(columns .Select(_generator.IndexModelBuilder) .Where(x => !string.IsNullOrWhiteSpace(x))); } var isEfCore3Plus = Settings.IsEfCore3Plus(); var data = new ContextModel { DbContextClassModifiers = Settings.DbContextClassModifiers, DbContextName = Settings.DbContextName, DbContextBaseClass = Settings.DbContextBaseClass, AddParameterlessConstructorToDbContext = Settings.AddParameterlessConstructorToDbContext, HasDefaultConstructorArgument = !string.IsNullOrEmpty(Settings.DefaultConstructorArgument), DefaultConstructorArgument = Settings.DefaultConstructorArgument, ConfigurationClassName = Settings.ConfigurationClassName, ConnectionString = Settings.ConnectionString, ConnectionStringName = Settings.ConnectionStringName, ConnectionStringActions = hasSpatialTypes && Settings.TemplateType != TemplateType.Ef6 ? ", x => x.UseNetTopologySuite()" : "", contextInterface = string.IsNullOrWhiteSpace(Settings.DbContextInterfaceName) ? "" : ", " + Settings.DbContextInterfaceName, setInitializer = string.Format("<{0}>(null);", Settings.DbContextName), DbContextClassIsPartial = Settings.DbContextClassIsPartial(), SqlCe = Settings.DatabaseType == DatabaseType.SqlCe, tables = _tables, hasTables = _hasTables, indexes = indexes, hasIndexes = indexes.Any(), storedProcs = _storedProcs, hasStoredProcs = _hasStoredProcs, tableValuedFunctionComplexTypes = _tableValuedFunctionComplexTypes, hasTableValuedFunctionComplexTypes = _hasTableValuedFunctionComplexTypes, AdditionalContextInterfaceItems = Settings.AdditionalContextInterfaceItems.Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList(), addSaveChanges = !Settings.UseInheritedBaseInterfaceFunctions, tableValuedFunctions = _tableValuedFunctions, scalarValuedFunctions = _scalarValuedFunctions, Sequences = _filter.Sequences, hasSequences = _filter.Sequences.Any(), hasTableValuedFunctions = _hasTableValuedFunctions && _filter.IncludeTableValuedFunctions, hasScalarValuedFunctions = _hasScalarValuedFunctions && _filter.IncludeScalarValuedFunctions, IncludeObjectContextConstructor = !Settings.DbContextBaseClass.Contains("IdentityDbContext"), QueryString = isEfCore3Plus ? "Set" : "Query", FromSql = isEfCore3Plus ? "FromSqlRaw" : "FromSql", ExecuteSqlCommand = isEfCore3Plus ? "ExecuteSqlRaw" : "ExecuteSqlCommand", StoredProcModelBuilderCommand = isEfCore3Plus ? "Entity" : "Query", StoredProcModelBuilderPostCommand = isEfCore3Plus ? ".HasNoKey()" : string.Empty, OnConfigurationUsesConfiguration = Settings.OnConfiguration == OnConfiguration.Configuration, OnConfigurationUsesConnectionString = Settings.OnConfiguration == OnConfiguration.ConnectionString, DefaultSchema = Settings.DefaultSchema, UseDatabaseProvider = Settings.DatabaseProvider(), SqlParameter = Settings.SqlParameter(), }; var co = new CodeOutput(string.Empty, filename, "Database context", _globalUsings); co.AddUsings(_template.DatabaseContextUsings(data)); co.AddCode(Template.Transform(_template.DatabaseContext(), data)); return(co); }
public CodeOutput GeneratePocoConfiguration(Table table) { var filename = table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName + Settings.FileExtension; if (!CanWritePocoConfiguration()) { FileManagementService.DeleteFile(filename); return(null); } var columns = table.Columns .Where(x => !x.Hidden && !string.IsNullOrEmpty(x.Config)) .OrderBy(x => x.Ordinal) .ToList(); var isEfCore3Plus = Settings.IsEfCore3Plus(); var foreignKeys = columns.SelectMany(x => x.ConfigFk).OrderBy(o => o).ToList(); var primaryKey = _generator.PrimaryKeyModelBuilder(table); var indexes = _generator.IndexModelBuilder(table); var hasIndexes = indexes != null && indexes.Any(); var data = new PocoConfigurationModel { UseHasNoKey = isEfCore3Plus && table.IsView && !table.HasPrimaryKey, Name = table.DbName, ToTableOrView = (isEfCore3Plus && table.IsView && !table.HasPrimaryKey) ? "ToView" : "ToTable", ConfigurationClassName = table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName, NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(), Schema = table.Schema.DbName, PrimaryKeyNameHumanCase = primaryKey ?? table.PrimaryKeyNameHumanCase(), HasSchema = !string.IsNullOrEmpty(table.Schema.DbName), ClassModifier = Settings.ConfigurationClassesModifiers, ClassComment = table.WriteComments(), Columns = columns.Select(x => x.Config).ToList(), HasReverseNavigation = table.ReverseNavigationProperty.Count > 0, UsesDictionary = table.UsesDictionary, ReverseNavigationProperty = table.ReverseNavigationProperty .OrderBy(x => x.Definition) .Select(x => new PocoReverseNavigationPropertyModel { ReverseNavHasComment = Settings.IncludeComments != CommentsStyle.None && !string.IsNullOrEmpty(x.Comments), ReverseNavComment = Settings.IncludeComments != CommentsStyle.None ? x.Comments : string.Empty, AdditionalReverseNavigationsDataAnnotations = Settings.AdditionalReverseNavigationsDataAnnotations, AdditionalDataAnnotations = x.AdditionalDataAnnotations, Definition = x.Definition }) .ToList(), HasForeignKey = foreignKeys.Any(), ForeignKeys = foreignKeys, MappingConfiguration = table.MappingConfiguration, ConfigurationClassesArePartial = Settings.ConfigurationClassesArePartial(), Indexes = indexes, HasIndexes = hasIndexes }; var co = new CodeOutput(table.DbName, filename, null, Settings.PocoConfigurationFolder, _globalUsings); co.AddUsings(_template.PocoConfigurationUsings(data)); co.AddCode(Template.Transform(_template.PocoConfiguration(), data)); return(co); }
public CodeOutput GenerateContext() { if (!CanWriteContext()) { return(null); } var indexes = new List <string>(); foreach (var table in _tables) { var columns = table.Table.Columns .Where(x => !x.Hidden && !string.IsNullOrEmpty(x.Config)) .OrderBy(x => x.Ordinal) .ToList(); indexes.AddRange(columns .Select(_generator.IndexModelBuilder) .Where(x => !string.IsNullOrEmpty(x))); } var isEfCore3 = Settings.TemplateType == TemplateType.EfCore3; var data = new ContextModel { DbContextClassModifiers = Settings.DbContextClassModifiers, DbContextName = Settings.DbContextName, DbContextBaseClass = Settings.DbContextBaseClass, AddParameterlessConstructorToDbContext = Settings.AddParameterlessConstructorToDbContext, HasDefaultConstructorArgument = !string.IsNullOrEmpty(Settings.DefaultConstructorArgument), DefaultConstructorArgument = Settings.DefaultConstructorArgument, ConfigurationClassName = Settings.ConfigurationClassName, ConnectionString = Settings.ConnectionString, contextInterface = string.IsNullOrWhiteSpace(Settings.DbContextInterfaceName) ? "" : ", " + Settings.DbContextInterfaceName, setInitializer = string.Format("<{0}>(null);", Settings.DbContextName), DbContextClassIsPartial = Settings.DbContextClassIsPartial(), SqlCe = Settings.DatabaseType == DatabaseType.SqlCe, tables = _tables, hasTables = _hasTables, indexes = indexes, hasIndexes = indexes.Any(), storedProcs = _storedProcs, hasStoredProcs = _hasStoredProcs, tableValuedFunctionComplexTypes = _tableValuedFunctionComplexTypes, hasTableValuedFunctionComplexTypes = _hasTableValuedFunctionComplexTypes, AdditionalContextInterfaceItems = Settings.AdditionalContextInterfaceItems.Where(x => !string.IsNullOrEmpty(x)).ToList(), addSaveChanges = !Settings.UseInheritedBaseInterfaceFunctions, tableValuedFunctions = _tableValuedFunctions, scalarValuedFunctions = _scalarValuedFunctions, hasTableValuedFunctions = _hasTableValuedFunctions && _filter.IncludeTableValuedFunctions, hasScalarValuedFunctions = _hasScalarValuedFunctions && _filter.IncludeScalarValuedFunctions, IncludeObjectContextConstructor = !Settings.DbContextBaseClass.Contains("IdentityDbContext"), QueryString = isEfCore3 ? "Set" : "Query", FromSql = isEfCore3 ? "FromSqlRaw" : "FromSql", ExecuteSqlCommand = isEfCore3 ? "ExecuteSqlRaw" : "ExecuteSqlCommand", StoredProcModelBuilderCommand = isEfCore3 ? "Entity" : "Query", StoredProcModelBuilderPostCommand = isEfCore3 ? ".HasNoKey()" : string.Empty }; var co = new CodeOutput(Settings.DbContextName + Settings.FileExtension, "Database context", GlobalUsings); co.AddUsings(Template.DatabaseContextUsings(data)); co.AddCode(Template.Transform(Template.DatabaseContext(), data)); return(co); }