1 //---------------------------------------------------------------------
2 // <copyright file="Item.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 namespace System.Data.EntityModel.SchemaObjectModel
12 using System.Collections.Generic;
13 using System.Data.Metadata.Edm;
17 /// Summary description for Item.
19 [System.Diagnostics.DebuggerDisplay("Name={Name}, BaseType={BaseType.FQName}, HasKeys={HasKeys}")]
20 internal sealed class SchemaEntityType : StructuredType
22 #region Private Fields
24 private const char KEY_DELIMITER = ' ';
25 private ISchemaElementLookUpTable<NavigationProperty> _navigationProperties = null;
26 private EntityKeyElement _keyElement = null;
27 private static List<PropertyRefElement> EmptyKeyProperties = new List<PropertyRefElement>(0);
29 #region Public Methods
34 /// <param name="parentElement"></param>
35 public SchemaEntityType(Schema parentElement)
38 if (Schema.DataModel == SchemaDataModelOption.EntityDataModel)
39 OtherContent.Add(Schema.SchemaSource);
43 #region Protected Methods
48 internal override void ResolveTopLevelNames()
50 base.ResolveTopLevelNames();
54 if (!(BaseType is SchemaEntityType))
56 AddError(ErrorCode.InvalidBaseType, EdmSchemaErrorSeverity.Error,
57 System.Data.Entity.Strings.InvalidBaseTypeForItemType(BaseType.FQName, FQName));
59 // Since the base type is not null, key must be defined on the base type
60 else if (_keyElement != null && BaseType != null)
62 AddError(ErrorCode.InvalidKey, EdmSchemaErrorSeverity.Error,
63 System.Data.Entity.Strings.InvalidKeyKeyDefinedInBaseClass(FQName, BaseType.FQName));
66 // If the base type is not null, then the key must be defined on the base entity type, since
67 // we don't allow entity type without keys.
68 else if (_keyElement == null)
70 AddError(ErrorCode.KeyMissingOnEntityType, EdmSchemaErrorSeverity.Error,
71 System.Data.Entity.Strings.KeyMissingOnEntityType(this.FQName));
73 else if (null == BaseType && null != UnresolvedBaseType)
75 // this is already an error situation, we won't do any resolve name further in this type
80 _keyElement.ResolveTopLevelNames();
85 #region Protected Properties
87 protected override bool HandleAttribute(XmlReader reader)
89 if (base.HandleAttribute(reader))
93 else if (CanHandleAttribute(reader, XmlConstants.OpenType) && Schema.DataModel == SchemaDataModelOption.EntityDataModel)
95 // EF does not support this EDM 3.0 attribute, so ignore it.
103 #region Private Methods
106 #region Public Properties
108 public EntityKeyElement KeyElement
119 public IList<PropertyRefElement> DeclaredKeyProperties
123 if (KeyElement == null)
125 return EmptyKeyProperties;
127 return KeyElement.KeyProperties;
135 public IList<PropertyRefElement> KeyProperties
139 if (KeyElement == null)
141 if (BaseType != null)
143 System.Diagnostics.Debug.Assert(BaseType is SchemaEntityType, "ItemType.BaseType is not ItemType");
144 return (BaseType as SchemaEntityType).KeyProperties;
147 return EmptyKeyProperties;
149 return this.KeyElement.KeyProperties;
156 public ISchemaElementLookUpTable<NavigationProperty> NavigationProperties
160 if (_navigationProperties == null)
162 _navigationProperties = new FilteredSchemaElementLookUpTable<NavigationProperty, SchemaElement>(NamedMembers);
164 return _navigationProperties;
169 #region Protected Methods
174 internal override void Validate()
176 // structured type base class will validate all members (properties, nav props, etc)
179 if (this.KeyElement != null)
181 this.KeyElement.Validate();
186 #region Protected Properties
188 protected override bool HandleElement(XmlReader reader)
190 if (base.HandleElement(reader))
194 else if (CanHandleElement(reader, XmlConstants.Key))
196 HandleKeyElement(reader);
199 else if (CanHandleElement(reader, XmlConstants.NavigationProperty))
201 HandleNavigationPropertyElement(reader);
204 else if (CanHandleElement(reader, XmlConstants.ValueAnnotation) && Schema.DataModel == SchemaDataModelOption.EntityDataModel)
206 // EF does not support this EDM 3.0 element, so ignore it.
210 else if (CanHandleElement(reader, XmlConstants.TypeAnnotation) && Schema.DataModel == SchemaDataModelOption.EntityDataModel)
212 // EF does not support this EDM 3.0 element, so ignore it.
220 #region Private Methods
225 /// <param name="reader"></param>
226 private void HandleNavigationPropertyElement(XmlReader reader)
228 NavigationProperty navigationProperty = new NavigationProperty(this);
229 navigationProperty.Parse(reader);
230 AddMember(navigationProperty);
236 /// <param name="reader"></param>
237 private void HandleKeyElement(XmlReader reader)
239 _keyElement = new EntityKeyElement(this);
240 _keyElement.Parse(reader);