Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Relationship.cs « SchemaObjectModel « EntityModel « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5091d573a397c5a11f8cd4869ff465200e113734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//---------------------------------------------------------------------
// <copyright file="Relationship.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

namespace System.Data.EntityModel.SchemaObjectModel
{
    using System.Collections.Generic;
    using System.Data.Metadata.Edm;
    using System.Data.Objects.DataClasses;
    using System.Diagnostics;
    using System.Xml;

    /// <summary>
    /// Represents an Association element
    /// </summary>
    internal sealed class Relationship : SchemaType, IRelationship
    {
        private RelationshipKind _relationshipKind;
        private RelationshipEndCollection _ends;
        private List<ReferentialConstraint> _constraints;
        private bool _isForeignKey;

        /// <summary>
        /// Construct a Relationship object
        /// </summary>
        /// <param name="parent">the parent</param>
        /// <param name="kind">the kind of relationship</param>
        public Relationship(Schema parent, RelationshipKind kind)
        : base(parent)
        {
            RelationshipKind = kind;

            if (Schema.DataModel == SchemaDataModelOption.EntityDataModel)
            {
                _isForeignKey = false;
                OtherContent.Add(Schema.SchemaSource);
            }
            else if (Schema.DataModel == SchemaDataModelOption.ProviderDataModel)
            {
                _isForeignKey = true;
            }
        }


        /// <summary>
        /// List of Ends defined for this Association
        /// </summary>
        public IList<IRelationshipEnd> Ends
        {
            get
            {
                if ( _ends == null )
                    _ends = new RelationshipEndCollection();
                return _ends;
            }
        }

        /// <summary>
        /// Returns the list of constraints on this relation
        /// </summary>
        public IList<ReferentialConstraint> Constraints
        {
            get
            {
                if (_constraints == null)
                {
                    _constraints = new List<ReferentialConstraint>();
                }
                return _constraints;
            }
        }

        public bool TryGetEnd( string roleName, out IRelationshipEnd end )
        {
            return _ends.TryGetEnd( roleName, out end );
        }


        /// <summary>
        /// Is this an Association
        /// </summary>
        public RelationshipKind RelationshipKind
        {
            get
            {
                return _relationshipKind;
            }
            private set
            {
                _relationshipKind = value;
            }
        }

        /// <summary>
        /// Is this a foreign key (aka foreign key) relationship?
        /// </summary>
        public bool IsForeignKey
        {
            get { return _isForeignKey; }
        }

        /// <summary>
        /// do whole element validation
        /// </summary>
        /// <returns></returns>
        internal override void Validate()
        {
            base.Validate();

            bool foundOperations = false;
            foreach ( RelationshipEnd end in Ends )
            {
                end.Validate();
                if ( RelationshipKind == RelationshipKind.Association )
                {
                    if ( end.Operations.Count > 0 )
                    {
                        if ( foundOperations )
                            end.AddError( ErrorCode.InvalidOperation, EdmSchemaErrorSeverity.Error, System.Data.Entity.Strings.InvalidOperationMultipleEndsInAssociation);
                        foundOperations = true;
                    }
                }
            }

            if (Constraints.Count == 0)
            {
                if (this.Schema.DataModel == SchemaDataModelOption.ProviderDataModel)
                {
                    AddError(ErrorCode.MissingConstraintOnRelationshipType,
                             EdmSchemaErrorSeverity.Error,
                             System.Data.Entity.Strings.MissingConstraintOnRelationshipType(FQName));
                }
            }
            else
            {
                foreach (ReferentialConstraint constraint in Constraints)
                {
                    constraint.Validate();
                }
            }
        }

        /// <summary>
        /// do whole element resolution
        /// </summary>
        internal override void ResolveTopLevelNames()
        {
            base.ResolveTopLevelNames();

            foreach ( RelationshipEnd end in Ends )
                end.ResolveTopLevelNames();

            foreach (ReferentialConstraint referentialConstraint in Constraints)
            {
                referentialConstraint.ResolveTopLevelNames();
            }
        }

        protected override bool HandleElement(XmlReader reader)
        {
            if (base.HandleElement(reader))
            {
                return true;
            }
            else if (CanHandleElement(reader, XmlConstants.End))
            {
                HandleEndElement(reader);
                return true;
            }
            else if (CanHandleElement(reader, XmlConstants.ReferentialConstraint))
            {
                HandleConstraintElement(reader);
                return true;
            }
            return false;
        }

        /// <summary>
        /// handle the End child element
        /// </summary>
        /// <param name="reader">XmlReader positioned at the end element</param>
        private void HandleEndElement(XmlReader reader)
        {
            Debug.Assert(reader != null);
            RelationshipEnd end = new RelationshipEnd(this);
            end.Parse(reader);

            if (Ends.Count == 2)
            {
                AddError( ErrorCode.InvalidAssociation, EdmSchemaErrorSeverity.Error, System.Data.Entity.Strings.TooManyAssociationEnds(FQName ) );
                return;
            }

            Ends.Add(end);
        }

        /// <summary>
        /// handle the constraint element
        /// </summary>
        /// <param name="reader">XmlReader positioned at the constraint element</param>
        private void HandleConstraintElement(XmlReader reader)
        {
            Debug.Assert(reader != null);

            ReferentialConstraint constraint = new ReferentialConstraint(this);
            constraint.Parse(reader);
            this.Constraints.Add(constraint);

            if (this.Schema.DataModel == SchemaDataModelOption.EntityDataModel && this.Schema.SchemaVersion >= XmlConstants.EdmVersionForV2)
            {
                // in V2, referential constraint implies foreign key
                _isForeignKey = true;
            }
        }

    }
}