Example #1
0
 /// <summary>
 /// Gets the <see cref="DataModelColumnAttribute"/> having the
 /// specified name and naming type.
 /// </summary>
 /// <param name="propertyNameOrColumnName"></param>
 /// <returns></returns>
 public DataModelColumnAttribute this[string propertyNameOrColumnName]
 {
     get
     {
         if (FieldMappings.ContainsKey(propertyNameOrColumnName))
         {
             return(FieldMappings[propertyNameOrColumnName]);
         }
         var ret = GetFieldMappingByDbColumnName(propertyNameOrColumnName);
         if (ret != null)
         {
             return(ret);
         }
         if (ForeignModelMappings.ContainsKey(propertyNameOrColumnName))
         {
             if (FieldMappings.ContainsKey(ForeignModelMappings[propertyNameOrColumnName].TargetMember.Name))
             {
                 return(FieldMappings[ForeignModelMappings[propertyNameOrColumnName].TargetMember.Name]);
             }
             var foreignMapping = ForeignModelMappings[propertyNameOrColumnName];
             var mapping        = FieldMappings.ToList().Find(fm => fm.Value.ColumnName == foreignMapping.LocalColumn).Value;
             return(mapping);
         }
         return(null);
     }
 }
 /// <summary>
 /// Updates the parent references of the data source and all child objects.
 /// </summary>
 public override void UpdateParentReferences(IConnection parentConnection)
 {
     base.UpdateParentReferences(parentConnection);
     KeyMappings.ToList().ForEach(keyMapping => keyMapping.UpdateParentReferences(this));
     FieldMappings.ToList().ForEach(fieldMapping => fieldMapping.UpdateParentReferences(this));
 }
Example #3
0
        private void ExecuteFieldMapBehaviors()
        {
            // Auto-infer IsIdentity if no field is declared as primary key
            // and no field is declared as identity and one member is named
            // "ID" and that member is an int or a long.
            bool hasPkOrIdent = false;
            DataModelColumnAttribute idFieldMapping = null;
            string typename = null;

            foreach (var field_kvp in FieldMappings)
            {
                var field = field_kvp.Value;
                if (!field.IsForeignKey)
                {
                    if ((field.IsIdentity && field.IsIdentityDefined) ||
                        (field.IsPrimaryKey && field.IsPrimaryKeyDefined))
                    {
                        hasPkOrIdent = true;
                    }
                    if (typename == null)
                    {
                        typename = field.TargetMember.DeclaringType.Name.ToLower();
                    }
                    var propname = field.TargetMember.Name.ToLower();
                    if (propname == "id" || propname == typename + "id" || propname == typename + "_id")
                    {
                        idFieldMapping = field;
                    }
                }
            }
            if (!hasPkOrIdent && idFieldMapping != null &&
                (idFieldMapping.DbType == DbType.Int32 ||
                 idFieldMapping.DbType == DbType.Int64))
            {
                idFieldMapping.IsIdentity = true;
            }

            // Auto-infer a dictionary entry placeholder for foreign entity mappings
            // with no referenced foreign key column
            foreach (var fk in ForeignModelMappings)
            {
                //if (fk.Value.TargetMemberType.IsOrInherits(typeof(IEnumerable))) continue;
                var tmt   = GetEntityType(fk.Value.TargetMemberType);
                var ttmap = GetEntityMapping(tmt);
                var ff    = ttmap != null
                             ? ttmap.GetFieldMappingByDbColumnName(fk.Value.RelatedTableColumn)
                             : null;

                if (FieldMappings.ToList().Exists(p => p
                                                  .Value.ColumnName.ToLower() == fk.Value.LocalColumn.ToLower()))
                {
                    continue;
                }
                var fm = new DataModelColumnAttribute();
                if (ff != null)
                {
                    ff.CopyDeltaTo(fm);
                    fm.ColumnName       = fk.Value.LocalColumn;
                    fm.DbType           = fk.Value.LocalColumnDbType;
                    fm.SqlDbType        = fk.Value.LocalColumnSqlDbType;
                    fm.ColumnSize       = fk.Value.LocalColumnSize;
                    fm.IsNullable       = fk.Value.LocalColumnIsNullable;
                    fm.TargetMember     = fk.Value.TargetMember;
                    fm.TargetMemberType = fk.Value.TargetMemberType;
                    fm.IsPrimaryKey     = false;
                    fm.IsIdentity       = false;
                    FieldMappings.Add("field:" + fk.Value.LocalColumn, fm);
                }
                _FieldMapBehaviorsExecuted = true;
            }

            // predetermine NULLables based on CLR nullability
            foreach (var field_kvp in FieldMappings)
            {
                var field = field_kvp.Value;
                if (!field.IsNullableDefined)
                {
                    var  memtype  = field.TargetMemberType;
                    bool nullable = !memtype.IsValueType;
                    if (memtype.IsGenericType && memtype.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        memtype  = memtype.GetGenericArguments()[0];
                        nullable = true;
                    }
                    field.IsNullable = (memtype.IsValueType || memtype == typeof(string)) && nullable && !field.IsPrimaryKey;
                }
            }
        }