Esempio n. 1
0
        private static object GetObjectKey(EntityMap entityMap, object entityObject, bool isPersisted)
        {
            FieldMap[] keyFields = entityMap.KeyFields;
            // Paul Welter (http://www.LoreSoft.com) -- allow inserts by using all columns as key
            if (keyFields.Length == 0 || entityMap.KeyType == KeyType.None)
            {
                keyFields = entityMap.Fields;
            }

            object[] keyArray = new object[keyFields.Length];

            for (int index = 0; index < keyFields.Length; index++)
            {
                string keyMember = keyFields[index].Member;
                object keyValue  = GetMember(entityMap, entityObject, keyMember);
                if (entityMap.KeyType == KeyType.None && keyValue == null)
                {
                    keyValue = string.Empty;
                }
                if (EntityKey.IsNull(keyValue, isPersisted))
                {
                    switch (entityMap.KeyType)
                    {
                    case KeyType.Auto: keyValue = EntityKey.GetAutoKey(keyValue, entityMap.Type); break;

                    case KeyType.Guid: keyValue = EntityKey.GetGuidKey(keyValue is Guid); break;

                    default: throw new ORMapperException("ObjectSpace: Entity Object is missing Key - " + entityMap.Type);
                    }
                    if (entityMap.HasHelper)
                    {
                        ((IObjectHelper)entityObject)[keyMember] = keyValue;
                    }
                    else
                    {
                        if (entityMap.Member(keyMember) is FieldInfo)
                        {
                            (entityMap.Member(keyMember) as FieldInfo).SetValue(entityObject, keyValue);
                        }
                        else
                        {
                            (entityMap.Member(keyMember) as PropertyInfo).SetValue(entityObject, keyValue, null);
                        }
                    }
                }

                keyArray[index] = keyValue;
            }

            if (keyArray.Length == 1)
            {
                return(keyArray[0]);
            }
            else
            {
                return(keyArray);
            }
        }