Example #1
0
        public override TableMetaInfo GetTableMetaInfo(Type type)
        {
            IModel      model      = db.Model;
            IEntityType entityType = model.FindEntityType(type);

            IEntityType[] allEntityTypes   = model.GetEntityTypes().ToArray();
            IProperty[]   entityProperties = entityType.GetProperties().ToArray();

            IRelationalEntityTypeAnnotations mapping = entityType.Relational();

            EFPropertyInfo[] pProperties = new EFPropertyInfo[entityProperties.Length];

            // Column info
            for (int i = 0; i < entityProperties.Length; i++)               // (var property in entityProperties) {
            {
                IProperty prop = entityProperties[i];

                IRelationalPropertyAnnotations rProp = prop.Relational();

                if (rProp == null || rProp.ColumnName.IsNulle())
                {
                    continue;
                }

                bool isIndex = prop.IsIndex();
                var  idx1    = prop.GetContainingKeys().ToArray();

                EFPropertyInfo pInfo = new EFPropertyInfo()
                {
                    EntityName   = prop.Name,
                    ColumnName   = rProp.ColumnName,
                    IsIdentity   = false,
                    IsKey        = prop.IsKey(),
                    IsIndex      = isIndex,
                    IsPrimaryKey = prop.IsPrimaryKey()
                };

                string columnType = rProp.ColumnType;

                pProperties[i] = pInfo;
            }
            ;


            var info = new TableMetaInfo()
            {
                TableName           = mapping.TableName,                                                                //table.Table, // watch out! not table.Name
                TableSchema         = mapping.Schema,                                                                   //table.Schema,
                TypeNameFull        = entityType.Name,                                                                  // or: == type.FullName,
                TypeName            = type.Name,
                TableColumnNames    = pProperties.Select(p => p.ColumnName).Where(n => n.NotNulle()).ToArray(),         //declaredProps.Select(dp => dp.Name).ToArray(),
                EntityPropertyNames = pProperties.Select(p => p.EntityName).Where(n => n.NotNulle()).ToArray(),         //mapped.Select(dd => dd.Name).ToArray()
                KeyColumnNames      = pProperties.Where(p => p != null && p.IsKey).Select(p => p.ColumnName).ToArray(), // table.ElementType.KeyMembers.Select(m => m.Name).ToArray(),
            };

            return(info);
        }
Example #2
0
        public string GetTableDefinitionCode(
            ITableMetaInfoBuilder tmiBldr,
            Type type,
            bool withNamespace = true)
        {
            TableMetaInfo tinfo = tmiBldr.GetTableMetaInfo(type);

            string code = GetStronglyTypedTableDefinitionCode(tinfo);

            if (withNamespace)
            {
                code = WrapTableDefinitionsCodeInOuterNamespaceAndUsings(new string[] { code });
            }

            return(code);
        }