Пример #1
0
        public override List <string> FakeDatabaseContextUsings(FakeContextModel data, IDbContextFilter filter)
        {
            var usings = new List <string>
            {
                "System",
                "System.Data",
                "System.Data.Common",
                "System.Data.Entity",
                "System.Data.Entity.Core.Objects",
                "System.Data.Entity.Infrastructure",
                "System.Data.Entity.Infrastructure.Interception",
                "System.Data.Entity.Infrastructure.Annotations",
                "System.Data.SqlClient",
                "System.Data.Entity.Spatial",
                "System.Data.SqlTypes",
                "System.Threading.Tasks",
                "System.Threading"
            };

            if (Settings.IncludeCodeGeneratedAttribute)
            {
                usings.Add("System.CodeDom.Compiler");
            }

            if (data.tables.Any() || data.hasStoredProcs)
            {
                usings.Add("System.Data.Entity");
                usings.Add("System.Linq");
            }

            if (data.hasStoredProcs)
            {
                usings.Add("System.Collections.Generic");
            }

            if (!Settings.UseInheritedBaseInterfaceFunctions)
            {
                usings.Add("System.Data.Entity");
                usings.Add("System.Data.Entity.Infrastructure");
                usings.Add("System.Collections.Generic");
                usings.Add("System.Data.Entity.Validation");
            }

            if (Settings.DatabaseType == DatabaseType.SqlCe)
            {
                usings.Add("System.Data.SqlClient");
                //usings.Add("System.DBNull");
                usings.Add("System.Data.SqlTypes");
            }

            return(usings);
        }
 public LatestForeignKeyNamingStrategy(IDbContextFilter filter, Table table)
     : base(filter, table)
 {
 }
Пример #3
0
        public CodeGenerator(Generator generator, IDbContextFilter filter)
        {
#pragma warning disable IDE0016 // Use 'throw' expression
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
#pragma warning restore IDE0016 // Use 'throw' expression

            var isEfCore  = Settings.GeneratorType == GeneratorType.EfCore;
            var isEfCore3 = Settings.TemplateType == TemplateType.EfCore3;

            _generator = generator;
            _filter    = filter;

            _tables = filter.Tables
                      .Where(t => !t.IsMapping && t.HasPrimaryKey)
                      .OrderBy(x => x.NameHumanCase)
                      .Select(tbl => new TableTemplateData(tbl))
                      .ToList();

            if (filter.IncludeStoredProcedures)
            {
                _storedProcs = filter.StoredProcs
                               .Where(s => s.IsStoredProcedure)
                               .OrderBy(x => x.NameHumanCase)
                               .Select(sp => new StoredProcTemplateData(
                                           sp.ReturnModels.Count > 0,
                                           sp.ReturnModels.Count == 1,
                                           sp.ReturnModels.Count > 1,
                                           sp.WriteStoredProcReturnType(_filter),
                                           sp.WriteStoredProcReturnModelName(filter),
                                           sp.WriteStoredProcFunctionName(),
                                           sp.WriteStoredProcFunctionParams(false),
                                           sp.WriteStoredProcFunctionParams(true),
                                           sp.StoredProcHasOutParams() || sp.ReturnModels.Count == 0,
                                           sp.WriteStoredProcFunctionOverloadCall(),
                                           sp.WriteStoredProcFunctionSetSqlParameters(false),
                                           sp.WriteStoredProcFunctionSetSqlParameters(true),
                                           sp.ReturnModels.Count == 1
                            ? // exec
                                           string.Format("EXEC @procResult = [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams()).Trim()
                            : string.Format("[{0}].[{1}]", sp.Schema.DbName, sp.DbName),
                                           sp.ReturnModels.Count == 1
                            ? // Async exec
                                           string.Format("EXEC [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams()).Trim()
                            : string.Format("[{0}].[{1}]", sp.Schema.DbName, sp.DbName),
                                           sp.WriteStoredProcReturnModelName(_filter),
                                           sp.WriteStoredProcFunctionSqlParameterAnonymousArray(true, true),
                                           sp.WriteStoredProcFunctionSqlParameterAnonymousArray(false, true),
                                           sp.WriteStoredProcFunctionDeclareSqlParameter(true),
                                           sp.WriteStoredProcFunctionDeclareSqlParameter(false),
                                           sp.Parameters.OrderBy(x => x.Ordinal).Select(sp.WriteStoredProcSqlParameterName).ToList(),
                                           sp.ReturnModels.Count,
                                           string.Format("EXEC @procResult = [{0}].[{1}] {2}", sp.Schema.DbName, sp.DbName, sp.WriteStoredProcFunctionSqlAtParams())
                                           ))
                               .ToList();
            }
            else
            {
                _storedProcs = new List <StoredProcTemplateData>();
            }

            if (filter.IncludeTableValuedFunctions)
            {
                _tableValuedFunctions = filter.StoredProcs
                                        .Where(s => s.IsTableValuedFunction)
                                        .OrderBy(x => x.NameHumanCase)
                                        .Select(tvf => new TableValuedFunctionsTemplateData(
                                                    tvf.ReturnModels.Count == 1 && tvf.ReturnModels[0].Count == 1,
                                                    tvf.ReturnModels.Count == 1 && tvf.ReturnModels[0].Count == 1 ? tvf.ReturnModels[0][0].ColumnName : null,
                                                    tvf.WriteStoredProcFunctionName(),
                                                    tvf.WriteStoredProcReturnModelName(_filter),
                                                    tvf.WriteStoredProcFunctionParams(false),
                                                    tvf.DbName,
                                                    tvf.Schema.DbName,
                                                    isEfCore ? tvf.WriteStoredProcFunctionDeclareSqlParameter(false) : tvf.WriteTableValuedFunctionDeclareSqlParameter(),
                                                    isEfCore
                            ? tvf.WriteStoredProcFunctionSqlParameterAnonymousArray(false, false)
                            : tvf.WriteTableValuedFunctionSqlParameterAnonymousArray(),
                                                    isEfCore ? tvf.WriteNetCoreTableValuedFunctionsSqlAtParams() : tvf.WriteStoredProcFunctionSqlAtParams(),
                                                    isEfCore3 ? "FromSqlRaw" : "FromSql",
                                                    isEfCore3 ? "Set" : "Query",
                                                    isEfCore3 ? "Entity" : "Query",
                                                    isEfCore3 ? ".HasNoKey()" : string.Empty
                                                    ))
                                        .ToList();

                _tableValuedFunctionComplexTypes = filter.StoredProcs
                                                   .Where(s => s.IsTableValuedFunction &&
                                                          !Settings.StoredProcedureReturnTypes.ContainsKey(s.NameHumanCase) &&
                                                          !Settings.StoredProcedureReturnTypes.ContainsKey(s.DbName))
                                                   .OrderBy(x => x.NameHumanCase)
                                                   .Select(x => x.WriteStoredProcReturnModelName(_filter))
                                                   .ToList();
            }
            else
            {
                _tableValuedFunctions            = new List <TableValuedFunctionsTemplateData>();
                _tableValuedFunctionComplexTypes = new List <string>();
            }

            if (filter.IncludeScalarValuedFunctions)
            {
                _scalarValuedFunctions = filter.StoredProcs
                                         .Where(s => s.IsScalarValuedFunction &&
                                                s.Parameters.Any(x => x.Mode == StoredProcedureParameterMode.Out))
                                         .OrderBy(x => x.NameHumanCase)
                                         .Select(svf => new ScalarValuedFunctionsTemplateData(
                                                     svf.WriteStoredProcFunctionName(),
                                                     svf.Parameters.Where(x => x.Mode == StoredProcedureParameterMode.Out).OrderBy(x => x.Ordinal).FirstOrDefault()?.PropertyType,
                                                     svf.WriteStoredProcFunctionParams(false),
                                                     svf.DbName,
                                                     svf.Schema.DbName
                                                     ))
                                         .ToList();
            }
            else
            {
                _scalarValuedFunctions = new List <ScalarValuedFunctionsTemplateData>();
            }


            _hasTables                          = _tables.Any();
            _hasStoredProcs                     = _storedProcs.Any();
            _hasTableValuedFunctions            = _tableValuedFunctions.Any();
            _hasScalarValuedFunctions           = _scalarValuedFunctions.Any();
            _hasTableValuedFunctionComplexTypes = _tableValuedFunctionComplexTypes.Any();
            _hasEnums = filter.Enums.Any();

            GlobalUsings = new List <string>();
            Template     = TemplateFactory.Create();
            CalcGlobalUsings();
        }
Пример #4
0
        public override List <string> FakeDatabaseContextUsings(FakeContextModel data, IDbContextFilter filter)
        {
            var usings = new List <string>
            {
                "System",
                "System.Data",
                "System.Threading.Tasks",
                "System.Threading",
                "Microsoft.EntityFrameworkCore.Infrastructure"
            };

            if (Settings.IncludeCodeGeneratedAttribute)
            {
                usings.Add("System.CodeDom.Compiler");
            }

            if (data.tables.Any() || data.hasStoredProcs)
            {
                usings.Add("System.Linq");
                usings.Add("Microsoft.EntityFrameworkCore");
            }

            if (data.hasStoredProcs)
            {
                usings.Add("System.Collections.Generic");
            }

            if (!Settings.UseInheritedBaseInterfaceFunctions)
            {
                usings.Add("System.Collections.Generic");
            }

            if (Settings.DatabaseType == DatabaseType.SqlCe)
            {
                usings.Add(Settings.IsEfCore3() ? "Microsoft.Data.SqlClient" : "System.Data.SqlClient");
                //usings.Add("System.DBNull");
                usings.Add("System.Data.SqlTypes");
            }

            return(usings);
        }
Пример #5
0
 public abstract List <string> FakeDatabaseContextUsings(FakeContextModel data, IDbContextFilter filter);
 public LegacyForeignKeyNamingStrategy(IDbContextFilter filter, Table table)
     : base(filter, table)
 {
     ReverseNavigationUniquePropNameClashes = new List <string>();
 }
        public Column CreateColumn(RawTable rt, Table table, IDbContextFilter filter)
        {
            var col = new Column
            {
                Scale               = rt.Scale,
                PropertyType        = GetPropertyType(rt.TypeName),
                SqlPropertyType     = rt.TypeName,
                IsNullable          = rt.IsNullable,
                MaxLength           = rt.MaxLength,
                DateTimePrecision   = rt.DateTimePrecision,
                Precision           = rt.Precision,
                IsIdentity          = rt.IsIdentity,
                IsComputed          = rt.IsComputed,
                IsRowGuid           = rt.IsRowGuid,
                GeneratedAlwaysType = (ColumnGeneratedAlwaysType)rt.GeneratedAlwaysType,
                IsStoreGenerated    = rt.IsStoreGenerated,
                PrimaryKeyOrdinal   = rt.PrimaryKeyOrdinal,
                IsPrimaryKey        = rt.PrimaryKey,
                IsForeignKey        = rt.IsForeignKey,
                IsSpatial           = rt.TypeName == "geography" || rt.TypeName == "geometry",
                Ordinal             = rt.Ordinal,
                DbName              = rt.ColumnName,
                Default             = rt.Default,
                ParentTable         = table,
                ExistsInBaseClass   = false
            };

            if (col.MaxLength == -1 && (col.SqlPropertyType.EndsWith("varchar", StringComparison.InvariantCultureIgnoreCase) ||
                                        col.SqlPropertyType.EndsWith("varbinary", StringComparison.InvariantCultureIgnoreCase)))
            {
                col.SqlPropertyType += "(max)";
            }

            if (col.IsPrimaryKey && !col.IsIdentity && col.IsStoreGenerated && rt.TypeName == "uniqueidentifier")
            {
                col.IsStoreGenerated = false;
                col.IsIdentity       = true;
            }

            if (!col.IsPrimaryKey && filter.IsExcluded(col))
            {
                col.Hidden = true;
            }

            col.IsFixedLength = (rt.TypeName == "char" || rt.TypeName == "nchar");
            col.IsUnicode     = !(rt.TypeName == "char" || rt.TypeName == "varchar" || rt.TypeName == "text");
            col.IsMaxLength   = (rt.TypeName == "ntext");

            col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && rt.TypeName == "timestamp";
            if (col.IsRowVersion)
            {
                col.MaxLength = 8;
            }

            if (rt.TypeName == "hierarchyid")
            {
                col.MaxLength = 0;
            }

            col.CleanUpDefault();
            col.NameHumanCase = CleanUp(col.DbName);
            if (string.IsNullOrWhiteSpace(col.NameHumanCase))
            {
                col.NameHumanCase = "Unknown";
                col.Hidden        = true;
            }
            col.NameHumanCase = ReservedColumnNames.Replace(col.NameHumanCase, "_$1");

            if (ReservedKeywords.Contains(col.NameHumanCase))
            {
                col.NameHumanCase = "@" + col.NameHumanCase;
            }

            col.DisplayName = Column.ToDisplayName(col.DbName);

            var titleCase = (Settings.UsePascalCase ? Inflector.ToTitleCase(col.NameHumanCase) : col.NameHumanCase).Replace(" ", string.Empty);

            if (titleCase != string.Empty)
            {
                col.NameHumanCase = titleCase;
            }

            // Make sure property name doesn't clash with class name
            if (col.NameHumanCase == table.NameHumanCase)
            {
                col.NameHumanCase += "_";
            }

            if (char.IsDigit(col.NameHumanCase[0]))
            {
                col.NameHumanCase = "_" + col.NameHumanCase;
            }

            table.HasNullableColumns = col.IsColumnNullable();

            // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types)
            if (string.IsNullOrEmpty(col.PropertyType))
            {
                return(null);
            }

            return(col);
        }
 public override List <string> FakeDatabaseContextUsings(FakeContextModel data, IDbContextFilter filter)
 {
     return(CacheList("FakeDatabaseContextUsings.txt"));
 }