Beispiel #1
0
        private static void ApplyConvention(MetaTable metaTable)
        {
            if (!metaTable.Columns.Any(c => c.IsKey))
            {
                var keyColumn = metaTable.Columns.FirstOrDefault(c => c.Member.Name.ToUpper() == "ID"
                                               || c.Member.Name.ToUpper() == (metaTable.EntityType.Name.ToUpper() + "ID"));

                if (keyColumn == null)
                    return;

                var memberDataType = GetMemberDataType(keyColumn.Member);
                var canBeIdentity = memberDataType == typeof(byte)
                                    || memberDataType == typeof(uint)
                                    || memberDataType == typeof(int)
                                    || memberDataType == typeof(ulong)
                                    || memberDataType == typeof(long);

                var isIdentity = canBeIdentity && (keyColumn.IsIdentity
                                                    || keyColumn.Member.GetCustomAttribute<DatabaseGeneratedAttribute>() == null);

                keyColumn.IsIdentity = isIdentity;
                keyColumn.IsKey = true;
            }
        }
Beispiel #2
0
        internal static MetaTable Create(Type entityType)
        {
            var tableAttr = GetTableAttribute(entityType);
            var metaTable = new MetaTable
            {
                EntityType = entityType,
                TableName = tableAttr.Name,
                Schema = tableAttr.Schema,
                Columns = (from m in entityType.GetMembers(BindingFlags.Public | BindingFlags.Instance)
                           where m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property
                           select MetaColumn.Create(m)).ToList()
            };

            ApplyConvention(metaTable);
            return metaTable;
        }