Exemple #1
0
        public List <ModelInfo> Generate(IEnumerable <ITable <TableContent, ColumnContent> > tables, IEnumerable <EntityInfo> entities)
        {
            var result     = new List <ModelInfo>();
            var tableArray = tables.ToArray();

            var baseModel       = TypeUsageInfo.CreateClass("BaseModel", string.Empty);
            var iintervalfields = TypeUsageInfo.CreateInterface("IIntervalModelFields", string.Empty);

            foreach (var entity in entities)
            {
                var table        = tableArray.First(t => t.Name == entity.TableName && t.Schema == entity.TableSchemaName);
                var modelColumns = table.Columns.Where(c => c.Content.InModel).ToArray();
                if (modelColumns.Length == 0)
                {
                    continue;
                }

                var properties = modelColumns.Select(c => new PropertyModelInfo(entity.SimpleProperties.First(p => p.ColumnName == c.Name), c)).ToList();
                var model      = new ModelInfo(entity, table, properties);

                model.Attributes.Add(AttributeDictionary.DataContract);
                model.InheritsFrom(baseModel);

                #region IntervalFields
                {
                    var fromDate = properties.FirstOrDefault(p => p.Name == "fromDate");
                    var toDate   = properties.FirstOrDefault(p => p.Name == "toDate");
                    if (fromDate != null && toDate != null)
                    {
                        entity.InheritsFrom(iintervalfields);

                        EntitiesManager.AddExplicitProperty(fromDate, iintervalfields, entity);
                        EntitiesManager.AddExplicitProperty(toDate, iintervalfields, entity);
                    }
                }
                #endregion

                foreach (var property in model.ModelProperties)
                {
                    if (property.Content.IsModelRequired)
                    {
                        property.Attributes.Add(AttributeDictionary.Required);
                    }
                    property.Attributes.Add(AttributeDictionary.DataMember);
                    property.Description = string.Format("Model property for <see cref=\"{0}.{1}\"/> entity", entity.Name, property.Property.Name);
                }

                result.Add(model);
            }

            return(result);
        }
        private static MSSQLTypeDescDictionary Initialize()
        {
            //NOTE: For type mappings see https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx
            var dictionary = new MSSQLTypeDescDictionary();

            dictionary.AddType <byte[]>(TypeIndexes.Image, "image", TypeOptions.IsLob);
            dictionary.AddType <string>(TypeIndexes.Text, "text", TypeOptions.HasCollation | TypeOptions.IsLob);
            dictionary.AddType <Guid>(TypeIndexes.UniqueIdentifier, "uniqueidentifier", TypeOptions.None);
            dictionary.AddType <byte>(TypeIndexes.TinyInt, "tinyint", TypeOptions.None);
            dictionary.AddType <short>(TypeIndexes.SmallInt, "smallint", TypeOptions.None);
            dictionary.AddType <int>(TypeIndexes.Int, "int", TypeOptions.None, "integer");
            dictionary.AddType <DateTime>(TypeIndexes.SmallDateTime, NativeTypes.SmallDateTime, "smalldatetime", TypeOptions.None);
            dictionary.AddType <float>(TypeIndexes.Real, NativeTypes.Real, "real", TypeOptions.None);
            dictionary.AddType <decimal>(TypeIndexes.Money, NativeTypes.Money, "money", TypeOptions.None);
            dictionary.AddType <DateTime>(TypeIndexes.DateTime, "datetime", TypeOptions.None);

            dictionary.AddType <double>(TypeIndexes.Float, "float", TypeOptions.HasPrecision, "Double precision");
            dictionary.AddType <string>(TypeIndexes.NText, "ntext", TypeOptions.HasCollation | TypeOptions.IsLob | TypeOptions.IsUnicode);
            dictionary.AddType <bool>(TypeIndexes.Bit, "bit", TypeOptions.None);
            dictionary.AddType <decimal>(TypeIndexes.Decimal, "decimal", TypeOptions.HasPrecision | TypeOptions.HasScale, "dec");
            dictionary.AddType <decimal>(TypeIndexes.Numeric, "numeric", TypeOptions.HasPrecision | TypeOptions.HasScale);
            dictionary.AddType <decimal>(TypeIndexes.SmallMoney, "smallmoney", TypeOptions.None);
            dictionary.AddType <long>(TypeIndexes.BigInt, "bigint", TypeOptions.None);
            dictionary.AddType <byte[]>(
                TypeIndexes.Varbinary, NativeTypes.Varbinary, "varbinary", TypeOptions.HasLength | TypeOptions.IsLobWithMax, "binary varying");
            dictionary.AddType <string>(TypeIndexes.Varchar,
                                        "varchar", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsLobWithMax, "char varying", "character varying");
            dictionary.AddType <byte[]>(TypeIndexes.Binary, "binary", TypeOptions.HasLength | TypeOptions.IsFixedLength);
            dictionary.AddType <string>(TypeIndexes.Char, "char", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsFixedLength);
            dictionary.AddType <byte[]>(TypeIndexes.TimeStamp, "timestamp", TypeOptions.None, "rowversion");
            dictionary.AddType <string>(
                TypeIndexes.NVarchar,
                "nvarchar",
                TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsLobWithMax | TypeOptions.IsUnicode,
                "nchar varying", "ncharacter varying", "national char varying", "national character varying");
            dictionary.AddType <string>(
                TypeIndexes.NChar,
                "nchar", TypeOptions.HasLength | TypeOptions.HasCollation | TypeOptions.IsUnicode | TypeOptions.IsFixedLength, "ncharacter", "national character");

            dictionary.AddType <string>(TypeIndexes.Sysname, NativeTypes.NVarchar, "sysname", TypeOptions.None);
            dictionary.AddType <string>(TypeIndexes.Xml, "xml", TypeOptions.IsLob);
            dictionary.AddType <DateTime>(TypeIndexes.Date, "date", TypeOptions.None);
            dictionary.AddType <DateTime>(TypeIndexes.Time, "time", TypeOptions.HasScale);
            dictionary.AddType <DateTime>(TypeIndexes.DateTime2, "datetime2", TypeOptions.HasScale);
            dictionary.AddType <DateTimeOffset>(TypeIndexes.DateTimeOffset, "datetimeoffset", TypeOptions.HasScale);


            dictionary.AddType(TypeIndexes.Geometry, NativeTypes.Clr, "geometry", TypeOptions.None, TypeUsageInfo.CreateClass("DbGeometry", "System.Data.Spatial"));
            dictionary.AddType(TypeIndexes.Geography, NativeTypes.Clr, "geography", TypeOptions.None, TypeUsageInfo.CreateClass("DbGeography", "System.Data.Spatial"));
            //Check this conversation for support http://entityframework.codeplex.com/discussions/415185
            dictionary.AddType(TypeIndexes.HierarchyId, NativeTypes.Clr, "hierarchyid", TypeOptions.None, TypeUsageInfo.CreateClass("SqlHierarchyId", "Microsoft.SqlServer.Types"));

            dictionary.AddType <object>(TypeIndexes.SqlVariant, "sql_variant", TypeOptions.None);
            dictionary.AddType <object>(TypeIndexes.Table, TypeOptions.IsVirtual);
            dictionary.AddType <object>(TypeIndexes.Clr, TypeOptions.IsVirtual);

            return(dictionary);
        }