Beispiel #1
0
        /// <summary>
        /// Creates a new instance of <see cref="TableReference"/>
        /// </summary>
        /// <param name="prop">The <see cref="PropertyInfo"/> to extract the metadata to be used to create the <see cref="TableReference"/> object</param>
        /// <returns>A new instance of <see cref="TableReference"/></returns>
        public static TableReference Create(PropertyInfo prop)
        {
            var entityType = GetReferenceTableEntityType(prop);

            return(new TableReference()
            {
                ReferenceTableKey = prop.GetCustomAttribute <TableReferenceAttribute>().ReferenceTableKey,
                EntityType = entityType,
                IsChild = typeof(IEnumerable).IsAssignableFrom(prop.PropertyType),
                ReferenceTable = DbTableSchema.Create(entityType),
                SourceColumn = DbColumnSchema.Create(prop)
            });
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new instance of <see cref="DbTableSchema"/>
        /// </summary>
        /// <param name="type">The entity <see cref="Type"/> to use to create the schema metadata</param>
        /// <returns>A new instance of <see cref="DbTableSchema"/></returns>
        public static DbTableSchema Create(Type type)
        {
            if (_cache.ContainsKey(type))
            {
                return(_cache[type]);
            }
            var columns = new List <DbColumnSchema>();
            var refs    = new List <TableReference>();
            var res     = new DbTableSchema()
            {
                TableName = GetTableName(type), Columns = columns, References = refs, EntityType = type
            };

            _cache[type] = res;

            foreach (var prop in type.GetProperties())
            {
                if (prop.GetCustomAttribute <NotMappedAttribute>() != null)
                {
                    continue;
                }
                if (prop.GetCustomAttribute <TableReferenceAttribute>() != null)
                {
                    var tableRef = TableReference.Create(prop);
                    ValidateTableRef(tableRef, res);
                    refs.Add(tableRef);
                    continue;
                }
                columns.Add(DbColumnSchema.Create(prop));
            }
            if (!res.Columns.Any(i => i.IsPrimaryKey) && res.Columns.Any(i => i.ColumnName == "Id"))
            {
                res.Columns.Single(i => i.ColumnName == "Id").IsPrimaryKey = true;
            }

            return(res);
        }