Ejemplo n.º 1
0
        }//method

        public static EntityRecord GetEntityRefTarget(EntityRecord record, EntityMemberInfo member)
        {
            // Current value is null (meaning not set, yet). We must figure it out.
            // 1. If corresponding FK is null, then value is DbNull.
            var fromKey = member.ReferenceInfo.FromKey;
            var toKey   = member.ReferenceInfo.ToKey;

            if (!record.KeyIsLoaded(fromKey) && record.Status != EntityStatus.New)
            {
                record.Reload();
            }
            if (record.KeyIsNull(fromKey))
            {
                return(null);
            }
            // 2. The key is not null, let's lookup the record by FK; maybe it was already loaded
            var fkValue      = new EntityKey(fromKey, record);
            var targetEntity = toKey.Entity;
            //Compute pkValue on target entity
            var pkValue = new EntityKey(targetEntity.PrimaryKey, fkValue.Values);
            //If Session available, ask session for existing record
            EntityRecord targetRec;

            if (record.Session != null)
            {
                targetRec = record.Session.GetRecord(pkValue, LoadFlags.Stub); //either already loaded or a stub
            }
            else
            {
                targetRec = new EntityRecord(pkValue); //create detached stub
            }
            return(targetRec);
        }//method