Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / ReferentialConstraint.cs
1 //---------------------------------------------------------------------
2 // <copyright file="ReferentialConstraint.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12 using System.Data.Common;
13
14 namespace System.Data.Metadata.Edm
15 {
16     /// <summary>
17     /// This class describes referential constraint on the relationships
18     /// </summary>
19     public sealed class ReferentialConstraint : MetadataItem
20     {
21         #region Constructors
22         /// <summary>
23         /// Constructs a new constraint on the relationship
24         /// </summary>
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)
34         {
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")));
41         }
42         #endregion
43
44         #region Fields
45         private RelationshipEndMember _fromRole;
46         private RelationshipEndMember _toRole;
47         private readonly ReadOnlyMetadataCollection<EdmProperty> _fromProperties;
48         private readonly ReadOnlyMetadataCollection<EdmProperty> _toProperties;
49         #endregion
50
51         #region Properties
52         /// <summary>
53         /// Returns the kind of the type
54         /// </summary>
55         public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.ReferentialConstraint; } }
56
57         /// <summary>
58         /// Returns the identity for this constraint
59         /// </summary>
60         internal override string Identity
61         {
62             get
63             {
64                 return this.FromRole.Name + "_" + this.ToRole.Name;
65             }
66         }
67
68         /// <summary>
69         /// Returns the FromRole which takes part in this referential constraint
70         /// </summary>
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
75         {
76             get
77             {
78                 return _fromRole;
79             }
80         }
81
82         /// <summary>
83         /// Returns the ToRole which takes part in this referential constraint
84         /// </summary>
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
89         {
90             get
91             {
92                 return _toRole;
93             }
94         }
95
96         /// <summary>
97         /// Returns the collection of properties on the from role on which the constraint is defined on
98         /// </summary>
99         [MetadataProperty(BuiltInTypeKind.EdmProperty, true)]
100         public ReadOnlyMetadataCollection<EdmProperty> FromProperties
101         {
102             get
103             {
104                 return _fromProperties;
105             }
106         }
107
108         /// <summary>
109         /// Returns the collection of properties on the ToRole on whose value the constraint is defined on
110         /// </summary>
111         [MetadataProperty(BuiltInTypeKind.EdmProperty, true)]
112         public ReadOnlyMetadataCollection<EdmProperty> ToProperties
113         {
114             get
115             {
116                 return _toProperties;
117             }
118         }
119         #endregion
120
121         #region Methods
122         /// <summary>
123         /// Overriding System.Object.ToString to provide better String representation 
124         /// for this type.
125         /// </summary>
126         public override string ToString()
127         {
128             return this.FromRole.Name + "_" + this.ToRole.Name;
129         }
130
131         /// <summary>
132         /// Sets this item to be read-only, once this is set, the item will never be writable again.
133         /// </summary>
134         internal override void SetReadOnly()
135         {
136             if (!IsReadOnly)
137             {
138                 base.SetReadOnly();
139
140                 RelationshipEndMember fromRole = FromRole;
141                 if (fromRole != null)
142                 {
143                     fromRole.SetReadOnly();
144                 }
145
146                 RelationshipEndMember toRole = ToRole;
147                 if (toRole != null)
148                 {
149                     toRole.SetReadOnly();
150                 }
151                 this.FromProperties.Source.SetReadOnly();
152                 this.ToProperties.Source.SetReadOnly();
153             }
154         }
155         #endregion
156     }
157 }