1 //---------------------------------------------------------------------
2 // <copyright file="ReferentialConstraint.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12 using System.Data.Common;
14 namespace System.Data.Metadata.Edm
17 /// This class describes referential constraint on the relationships
19 public sealed class ReferentialConstraint : MetadataItem
23 /// Constructs a new constraint on the relationship
25 /// <param name="fromRole">role from which the relationship originates</param>
26 /// <param name="toRole">role to which the relationship is linked/targeted to</param>
27 /// <param name="toProperties">properties on entity type of from role which take part in the constraint</param>
28 /// <param name="fromProperties">properties on entity type of to role which take part in the constraint</param>
29 /// <exception cref="ArgumentNullException">Argument Null exception if any of the arguments is null</exception>
30 internal ReferentialConstraint(RelationshipEndMember fromRole,
31 RelationshipEndMember toRole,
32 IEnumerable<EdmProperty> fromProperties,
33 IEnumerable<EdmProperty> toProperties)
35 _fromRole = EntityUtil.GenericCheckArgumentNull(fromRole, "fromRole");
36 _toRole = EntityUtil.GenericCheckArgumentNull(toRole, "toRole");
37 _fromProperties = new ReadOnlyMetadataCollection<EdmProperty>(new MetadataCollection<EdmProperty>(
38 EntityUtil.GenericCheckArgumentNull(fromProperties, "fromProperties")));
39 _toProperties = new ReadOnlyMetadataCollection<EdmProperty>(new MetadataCollection<EdmProperty>(
40 EntityUtil.GenericCheckArgumentNull(toProperties, "toProperties")));
45 private RelationshipEndMember _fromRole;
46 private RelationshipEndMember _toRole;
47 private readonly ReadOnlyMetadataCollection<EdmProperty> _fromProperties;
48 private readonly ReadOnlyMetadataCollection<EdmProperty> _toProperties;
53 /// Returns the kind of the type
55 public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.ReferentialConstraint; } }
58 /// Returns the identity for this constraint
60 internal override string Identity
64 return this.FromRole.Name + "_" + this.ToRole.Name;
69 /// Returns the FromRole which takes part in this referential constraint
71 /// <exception cref="System.ArgumentNullException">Thrown if value passed into setter is null</exception>
72 /// <exception cref="System.InvalidOperationException">Thrown if the ReferentialConstraint instance is in ReadOnly state</exception>
73 [MetadataProperty(BuiltInTypeKind.RelationshipEndMember, false)]
74 public RelationshipEndMember FromRole
83 /// Returns the ToRole which takes part in this referential constraint
85 /// <exception cref="System.ArgumentNullException">Thrown if value passed into setter is null</exception>
86 /// <exception cref="System.InvalidOperationException">Thrown if the ReferentialConstraint instance is in ReadOnly state</exception>
87 [MetadataProperty(BuiltInTypeKind.RelationshipEndMember, false)]
88 public RelationshipEndMember ToRole
97 /// Returns the collection of properties on the from role on which the constraint is defined on
99 [MetadataProperty(BuiltInTypeKind.EdmProperty, true)]
100 public ReadOnlyMetadataCollection<EdmProperty> FromProperties
104 return _fromProperties;
109 /// Returns the collection of properties on the ToRole on whose value the constraint is defined on
111 [MetadataProperty(BuiltInTypeKind.EdmProperty, true)]
112 public ReadOnlyMetadataCollection<EdmProperty> ToProperties
116 return _toProperties;
123 /// Overriding System.Object.ToString to provide better String representation
126 public override string ToString()
128 return this.FromRole.Name + "_" + this.ToRole.Name;
132 /// Sets this item to be read-only, once this is set, the item will never be writable again.
134 internal override void SetReadOnly()
140 RelationshipEndMember fromRole = FromRole;
141 if (fromRole != null)
143 fromRole.SetReadOnly();
146 RelationshipEndMember toRole = ToRole;
149 toRole.SetReadOnly();
151 this.FromProperties.Source.SetReadOnly();
152 this.ToProperties.Source.SetReadOnly();