Beispiel #1
0
        private static DataMap GenerateMapInternal(Type type, string tableName, IEnumerable <string> keyFields, int schemaVersion)
        {
            if (schemaVersion == 0)
            {
                schemaVersion = int.MaxValue;
            }

            if (keyFields == null)
            {
                keyFields = new string[0];
            }

            var map = new DataMap(type);

            // get or create data item
            IDataMapItem theDataItem = DataItemAttribute.GetFromTypeOrMember(type);

            if (theDataItem != null)
            {
                if (!string.IsNullOrEmpty(tableName))
                {
                    theDataItem.TableName = tableName;
                }
            }
            else
            {
                theDataItem = new DataMapItem(tableName, true);
            }

            // assign to data map
            map.DataItem = theDataItem;

            // create fields from properties
            foreach (var pi in type.GetProperties())
            {
                IDataMapField field = DataFieldAttribute.GetFromProperty(pi);

                if ((field == null) && theDataItem.ImplicitFieldDefinition)
                {
                    field = new DataMapField(pi, pi.Name, DataManager.GetDbType(pi.PropertyType), true);

                    if (keyFields.Contains(field.FieldName, StringComparer.OrdinalIgnoreCase))
                    {
                        field.IsKeyField = true;
                    }

                    // special handling of rowVersion field
                    if ((pi.PropertyType == typeof(byte[])) && field.FieldName.Equals("rowVersion", StringComparison.OrdinalIgnoreCase))
                    {
                        field.IsRowVersion = true;
                    }

                    // special handling of UTC fields (not in 4.0)
                    if (((pi.PropertyType == typeof(DateTime)) || (pi.PropertyType == typeof(DateTime?))) && field.FieldName.EndsWith("UTC", StringComparison.OrdinalIgnoreCase))
                    {
                        field.DateTimeKind = DateTimeKind.Utc;
                    }
                }

                if ((field != null) && (field.SchemaVersion <= schemaVersion))
                {
                    map.AppendField(field);
                }
            }

            return(map);
        }