1 //------------------------------------------------------------------------------
2 // <copyright file="EntityRecordInfo.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8 
9 namespace System.Data.Common {
10 
11     using System.Collections.Generic;
12     using System.Data;
13     using System.Data.Metadata.Edm;
14     using System.Diagnostics;
15 
16     /// <summary>
17     /// EntityRecordInfo class providing a simple way to access both the type information and the column information.
18     /// </summary>
19     public class EntityRecordInfo : DataRecordInfo {
20 
21         private readonly EntityKey _entityKey;
22         private readonly EntitySet _entitySet;
23 
24         /// <summary>
25         ///
26         /// </summary>
27         /// <param name="metadata"></param>
28         /// <param name="memberInfo"></param>
29         /// <param name="entityKey"></param>
EntityRecordInfo(EntityType metadata, IEnumerable<EdmMember> memberInfo, EntityKey entityKey, EntitySet entitySet)30         public EntityRecordInfo(EntityType metadata, IEnumerable<EdmMember> memberInfo, EntityKey entityKey, EntitySet entitySet)
31             : base(TypeUsage.Create(metadata), memberInfo) {
32             EntityUtil.CheckArgumentNull<EntityKey>(entityKey, "entityKey");
33             EntityUtil.CheckArgumentNull(entitySet, "entitySet");
34 
35             _entityKey = entityKey;
36             _entitySet = entitySet;
37             ValidateEntityType(entitySet);
38         }
39 
40         /// <summary>
41         ///
42         /// </summary>
43         /// <param name="metadata"></param>
44         /// <param name="entityKey"></param>
EntityRecordInfo(EntityType metadata, EntityKey entityKey, EntitySet entitySet)45         internal EntityRecordInfo(EntityType metadata, EntityKey entityKey, EntitySet entitySet)
46             : base(TypeUsage.Create(metadata)) {
47             EntityUtil.CheckArgumentNull<EntityKey>(entityKey, "entityKey");
48 
49             _entityKey = entityKey;
50             _entitySet = entitySet;
51 #if DEBUG
52             try
53             {
54                 ValidateEntityType(entitySet);
55             }
56             catch
57             {
58                 Debug.Assert(false, "should always be valid EntityType when internally constructed");
59                 throw;
60             }
61 #endif
62         }
63 
64         /// <summary>
65         /// Reusing TypeUsage and FieldMetadata from another EntityRecordInfo which has all the same info
66         /// but with a different EntityKey instance.
67         /// </summary>
EntityRecordInfo(DataRecordInfo info, EntityKey entityKey, EntitySet entitySet)68         internal EntityRecordInfo(DataRecordInfo info, EntityKey entityKey, EntitySet entitySet)
69             : base(info)
70         {
71             _entityKey = entityKey;
72             _entitySet = entitySet;
73 #if DEBUG
74             try
75             {
76                 ValidateEntityType(entitySet);
77             }
78             catch
79             {
80                 Debug.Assert(false, "should always be valid EntityType when internally constructed");
81                 throw;
82             }
83 #endif
84         }
85 
86         /// <summary>
87         /// the EntityKey
88         /// </summary>
89         public EntityKey EntityKey {
90             get {
91                 return _entityKey;
92             }
93         }
94 
95         // using EntitySetBase versus EntitySet prevents the unnecessary cast of ElementType to EntityType
ValidateEntityType(EntitySetBase entitySet)96         private void ValidateEntityType(EntitySetBase entitySet)
97         {
98             if (!object.ReferenceEquals(RecordType.EdmType, null) &&
99                 !object.ReferenceEquals(_entityKey, EntityKey.EntityNotValidKey) &&
100                 !object.ReferenceEquals(_entityKey, EntityKey.NoEntitySetKey) &&
101                 !object.ReferenceEquals(RecordType.EdmType, entitySet.ElementType) &&
102                 !entitySet.ElementType.IsBaseTypeOf(RecordType.EdmType))
103             {
104                 throw EntityUtil.Argument(System.Data.Entity.Strings.EntityTypesDoNotAgree);
105             }
106         }
107     }
108 }
109