internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo mi, int ordinal)
        {
            this.declaringType  = mi.DeclaringType;
            this.metaType       = metaType;
            this.member         = mi;
            this.ordinal        = ordinal;
            this.type           = TypeSystem.GetMemberType(mi);
            this.isNullableType = TypeSystem.IsNullableType(this.type);
            this.attrColumn     = (ColumnAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnAttribute));
            this.attrAssoc      = (AssociationAttribute)Attribute.GetCustomAttribute(mi, typeof(AssociationAttribute));
            this.attr           = (this.attrColumn != null) ? (DataAttribute)this.attrColumn : (DataAttribute)this.attrAssoc;
            if (this.attr != null && this.attr.Storage != null)
            {
                MemberInfo[] mis = mi.DeclaringType.GetMember(this.attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic);
                if (mis == null || mis.Length != 1)
                {
                    throw Error.BadStorageProperty(this.attr.Storage, mi.DeclaringType, mi.Name);
                }
                this.storageMember = mis[0];
            }
            Type storageType = this.storageMember != null?TypeSystem.GetMemberType(this.storageMember) : this.type;

            this.isDeferred = IsDeferredType(storageType);
            if (attrColumn != null && attrColumn.IsDbGenerated && attrColumn.IsPrimaryKey)
            {
                // auto-gen identities must be synced on insert
                if ((attrColumn.AutoSync != AutoSync.Default) && (attrColumn.AutoSync != AutoSync.OnInsert))
                {
                    throw Error.IncorrectAutoSyncSpecification(mi.Name);
                }
            }
        }
		private static string AttributeNameNullCheck (AssociationAttribute attribute)
		{
			if ( attribute == null )
				return null;
			else
				return attribute.Name;
		}
 public AttributedMetaAssociation(MemberInfo member, AssociationAttribute attribute, MetaDataMember metaDataMember)
 {
     memberInfo = member;
     associationAttribute = attribute;
     thisMember = metaDataMember;
     Load();
 }
Beispiel #4
0
        internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo member, int ordinal)
        {
            declaringType        = member.DeclaringType;
            this.metaType        = metaType;
            this.member          = member;
            this.ordinal         = ordinal;
            type                 = TypeSystem.GetMemberType(member);
            isNullableType       = TypeSystem.IsNullableType(type);
            columnAttribute      = ((AttributedMetaModel)metaType.Model).TryGetColumnAttribute(member);
            associationAttribute = ((AttributedMetaModel)metaType.Model).TryGetAssociationAttribute(member);
            var attr = (columnAttribute == null) ? associationAttribute : (DataAttribute)columnAttribute;

            if (attr != null && attr.Storage != null)
            {
                var mis = member.DeclaringType.GetMember(attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic);
                if (mis.Length != 1)
                {
                    throw Error.BadStorageProperty(attr.Storage, member.DeclaringType, member.Name);
                }
                storageMember = mis[0];
            }
            var storageType = storageMember == null ? type : TypeSystem.GetMemberType(storageMember);

            isDeferred = ((AttributedMetaModel)metaType.Model).IsDeferredMember(member, storageType, associationAttribute);

            // auto-gen identities must be synced on insert
            if ((columnAttribute != null) &&
                columnAttribute.IsDbGenerated &&
                columnAttribute.IsPrimaryKey &&
                (columnAttribute.AutoSync != AutoSync.Default) &&
                (columnAttribute.AutoSync != AutoSync.OnInsert))
            {
                throw Error.IncorrectAutoSyncSpecification(member.Name);
            }
        }
		internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo mi, int ordinal)
		{
			this.declaringType = mi.DeclaringType;
			this.metaType = metaType;
			this.member = mi;
			this.ordinal = ordinal;
			this.type = TypeSystem.GetMemberType(mi);
			this.isNullableType = TypeSystem.IsNullableType(this.type);
			this.attrColumn = (ColumnAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnAttribute));
			this.attrAssoc = (AssociationAttribute)Attribute.GetCustomAttribute(mi, typeof(AssociationAttribute));
			this.attr = (this.attrColumn != null) ? (DataAttribute)this.attrColumn : (DataAttribute)this.attrAssoc;
			if(this.attr != null && this.attr.Storage != null)
			{
				MemberInfo[] mis = mi.DeclaringType.GetMember(this.attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic);
				if(mis == null || mis.Length != 1)
				{
					throw Error.BadStorageProperty(this.attr.Storage, mi.DeclaringType, mi.Name);
				}
				this.storageMember = mis[0];
			}
			Type storageType = this.storageMember != null ? TypeSystem.GetMemberType(this.storageMember) : this.type;
			this.isDeferred = IsDeferredType(storageType);
			if(attrColumn != null && attrColumn.IsDbGenerated && attrColumn.IsPrimaryKey)
			{
				// auto-gen identities must be synced on insert
				if((attrColumn.AutoSync != AutoSync.Default) && (attrColumn.AutoSync != AutoSync.OnInsert))
				{
					throw Error.IncorrectAutoSyncSpecification(mi.Name);
				}
			}
		}
 private static void CopyAttributeToMapping(AssociationAttribute attr, IPropertyMapping belongsTo)
 {
     if (attr.IsForeignKey) belongsTo.Attributes[Constants.IsForeignKey] = "true";
     if (!string.IsNullOrEmpty(attr.ThisKey)) belongsTo.Attributes[Constants.ThisKey] = attr.ThisKey;
     if (!string.IsNullOrEmpty(attr.OtherKey)) belongsTo.Attributes[Constants.OtherKey] = attr.OtherKey;
     if(!string.IsNullOrEmpty(attr.DeleteRule)) belongsTo.Attributes[Constants.DeleteRule] = attr.DeleteRule;
     if(!string.IsNullOrEmpty(attr.Storage)) belongsTo.Attributes[Constants.Storage] = attr.Storage;
 }
        internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
        {
            thisMember = member;

            isMany = TypeSystem.IsSequenceType(thisMember.Type);
            var otherEntityType = isMany ? TypeSystem.GetElementType(thisMember.Type) : thisMember.Type;

            otherType = thisMember.DeclaringType.Model.GetMetaType(otherEntityType);
            thisKey   = attr.ThisKey == null ?
                        thisMember.DeclaringType.IdentityMembers :
                        MakeKeys(thisMember.DeclaringType, attr.ThisKey);
            otherKey             = attr.OtherKey == null ? otherType.IdentityMembers : MakeKeys(otherType, attr.OtherKey);
            thisKeyIsPrimaryKey  = AreEqual(thisKey, thisMember.DeclaringType.IdentityMembers);
            otherKeyIsPrimaryKey = AreEqual(otherKey, otherType.IdentityMembers);
            isForeignKey         = attr.IsForeignKey;

            isUnique     = attr.IsUnique;
            deleteRule   = attr.DeleteRule;
            deleteOnNull = attr.DeleteOnNull;

            // if any key members are not nullable, the association is not nullable
            isNullable = true;
            foreach (var thisKeyMember in thisKey)
            {
                if (!thisKeyMember.CanBeNull)
                {
                    isNullable = false;
                    break;
                }
            }

            // validate DeleteOnNull specification
            if (deleteOnNull && (!isForeignKey || isMany || isNullable))
            {
                throw Error.InvalidDeleteOnNullSpecification(member);
            }

            if ((thisKey.Count != otherKey.Count) && (thisKey.Count > 0) && (otherKey.Count > 0))
            {
                throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
            }

            // determine reverse reference member
            foreach (var otherTypePersistentMember in otherType.PersistentDataMembers)
            {
                var otherTypeMemberAssociationAttribute = ((AttributedMetaModel)thisMember.DeclaringType.Model)
                                                          .TryGetAssociationAttribute(otherTypePersistentMember.Member);
                if ((otherTypeMemberAssociationAttribute != null) &&
                    (otherTypePersistentMember != thisMember) &&
                    (otherTypeMemberAssociationAttribute.Name == attr.Name))
                {
                    otherMember = otherTypePersistentMember;
                    break;
                }
            }

            //validate the number of ThisKey columns is the same as the number of OtherKey columns
        }
Beispiel #8
0
        internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
        {
            this.thisMember = member;

            this.isMany = TypeSystem.IsSequenceType(this.thisMember.Type);
            Type ot = this.isMany ? TypeSystem.GetElementType(this.thisMember.Type) : this.thisMember.Type;

            this.otherType            = this.thisMember.DeclaringType.Model.GetMetaType(ot);
            this.thisKey              = (attr.ThisKey != null) ? MakeKeys(this.thisMember.DeclaringType, attr.ThisKey) : this.thisMember.DeclaringType.IdentityMembers;
            this.otherKey             = (attr.OtherKey != null) ? MakeKeys(otherType, attr.OtherKey) : this.otherType.IdentityMembers;
            this.thisKeyIsPrimaryKey  = AreEqual(this.thisKey, this.thisMember.DeclaringType.IdentityMembers);
            this.otherKeyIsPrimaryKey = AreEqual(this.otherKey, this.otherType.IdentityMembers);
            this.isForeignKey         = attr.IsForeignKey;

            this.isUnique     = attr.IsUnique;
            this.deleteRule   = attr.DeleteRule;
            this.deleteOnNull = attr.DeleteOnNull;

            // if any key members are not nullable, the association is not nullable
            foreach (MetaDataMember mm in thisKey)
            {
                if (!mm.CanBeNull)
                {
                    this.isNullable = false;
                    break;
                }
            }

            // validate DeleteOnNull specification
            if (deleteOnNull == true)
            {
                if (!(isForeignKey && !isMany && !isNullable))
                {
                    throw Error.InvalidDeleteOnNullSpecification(member);
                }
            }

            //validate the number of ThisKey columns is the same as the number of OtherKey columns
            if (this.thisKey.Count != this.otherKey.Count && this.thisKey.Count > 0 && this.otherKey.Count > 0)
            {
                throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
            }

            // determine reverse reference member
            foreach (MetaDataMember omm in this.otherType.PersistentDataMembers)
            {
                AssociationAttribute oattr = (AssociationAttribute)Attribute.GetCustomAttribute(omm.Member, typeof(AssociationAttribute));
                if (oattr != null)
                {
                    if (omm != this.thisMember && oattr.Name == attr.Name)
                    {
                        this.otherMember = omm;
                        break;
                    }
                }
            }
        }
		internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
		{
			this.thisMember = member;

			this.isMany = TypeSystem.IsSequenceType(this.thisMember.Type);
			Type ot = this.isMany ? TypeSystem.GetElementType(this.thisMember.Type) : this.thisMember.Type;
			this.otherType = this.thisMember.DeclaringType.Model.GetMetaType(ot);
			this.thisKey = (attr.ThisKey != null) ? MakeKeys(this.thisMember.DeclaringType, attr.ThisKey) : this.thisMember.DeclaringType.IdentityMembers;
			this.otherKey = (attr.OtherKey != null) ? MakeKeys(otherType, attr.OtherKey) : this.otherType.IdentityMembers;
			this.thisKeyIsPrimaryKey = AreEqual(this.thisKey, this.thisMember.DeclaringType.IdentityMembers);
			this.otherKeyIsPrimaryKey = AreEqual(this.otherKey, this.otherType.IdentityMembers);
			this.isForeignKey = attr.IsForeignKey;

			this.isUnique = attr.IsUnique;
			this.deleteRule = attr.DeleteRule;
			this.deleteOnNull = attr.DeleteOnNull;

			// if any key members are not nullable, the association is not nullable
			foreach(MetaDataMember mm in thisKey)
			{
				if(!mm.CanBeNull)
				{
					this.isNullable = false;
					break;
				}
			}

			// validate DeleteOnNull specification
			if(deleteOnNull == true)
			{
				if(!(isForeignKey && !isMany && !isNullable))
				{
					throw Error.InvalidDeleteOnNullSpecification(member);
				}
			}

			//validate the number of ThisKey columns is the same as the number of OtherKey columns
			if(this.thisKey.Count != this.otherKey.Count && this.thisKey.Count > 0 && this.otherKey.Count > 0)
			{
				throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
			}

			// determine reverse reference member
			foreach(MetaDataMember omm in this.otherType.PersistentDataMembers)
			{
				AssociationAttribute oattr = (AssociationAttribute)Attribute.GetCustomAttribute(omm.Member, typeof(AssociationAttribute));
				if(oattr != null)
				{
					if(omm != this.thisMember && oattr.Name == attr.Name)
					{
						this.otherMember = omm;
						break;
					}
				}
			}
		}
Beispiel #10
0
        internal virtual bool IsDeferredMember(MemberInfo member, Type storageType, AssociationAttribute associationAttribute)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }
            if (storageType == null)
            {
                throw new ArgumentNullException("storageType");
            }

            return(IsDeferredType(storageType));
        }
Beispiel #11
0
        internal virtual bool DoesMemberRequireProxy(
            MemberInfo member,
            Type storageType,
            AssociationAttribute associationAttribute)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }
            if (storageType == null)
            {
                throw new ArgumentNullException("storageType");
            }

            return(false);
        }
        public AttributedMetaAssociation(MemberInfo member, AssociationAttribute attribute, MetaDataMember metaDataMember)
        {
            _memberInfo = member;
            _associationAttribute = attribute;
            _thisMember = metaDataMember;
			// The bug described here:
			// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=376669
			// says that under certain conditions, there's a bug where _otherMember == _thisMember
			// Not only is there no point in reproducing a MS bug, it's not as simple as simply setting _otherMember = metaDataMember
			Type otherType = _memberInfo.GetFirstInnerReturnType();
			string associationName = member.GetAttribute<AssociationAttribute>().Name;
			AttributedMetaType ownedMetaType = metaDataMember.DeclaringType.Model.GetMetaType(otherType) as AttributedMetaType;

			if ( ownedMetaType == null )
				throw new InvalidOperationException("Key in referenced table is of a different SQL MetaData provider");

			_otherMember = ownedMetaType.AssociationsLookup[otherType.GetMembers().Where(m => (AttributeNameNullCheck(m.GetAttribute<AssociationAttribute>()) == associationName) && (m != member)).Single()];
        }
 public AttributedAssociationMetaDataMember(MemberInfo member, AssociationAttribute attribute, MetaType declaringType)
     : base(member, declaringType, attribute.Storage != null ? member.DeclaringType.GetSingleMember(attribute.Storage) : null)
 {
     associationAttribute = attribute;
 }
 public AttributedAssociationMetaDataMember(MemberInfo member, AssociationAttribute attribute, MetaType declaringType)
     : base(member, declaringType, attribute)
 {
     associationAttribute = attribute;
 }