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
|
//---------------------------------------------------------------------
// <copyright file="EntitySet.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Data.Common.Utils;
using System.Collections.ObjectModel;
namespace System.Data.Metadata.Edm
{
/// <summary>
/// concrete Class for representing a entity set
/// </summary>
public class EntitySet : EntitySetBase
{
#region Constructors
/// <summary>
/// The constructor for constructing the EntitySet with a given name and an entity type
/// </summary>
/// <param name="name">The name of the EntitySet</param>
/// <param name="schema">The db schema</param>
/// <param name="table">The db table</param>
/// <param name="definingQuery">The provider specific query that should be used to retrieve the EntitySet</param>
/// <param name="entityType">The entity type of the entities that this entity set type contains</param>
/// <exception cref="System.ArgumentNullException">Thrown if the argument name or entityType is null</exception>
internal EntitySet(string name, string schema, string table, string definingQuery, EntityType entityType)
: base(name, schema, table, definingQuery, entityType)
{
}
#endregion
#region Fields
private ReadOnlyCollection<Tuple<AssociationSet, ReferentialConstraint>> _foreignKeyDependents;
private ReadOnlyCollection<Tuple<AssociationSet, ReferentialConstraint>> _foreignKeyPrincipals;
private volatile bool _hasForeignKeyRelationships;
private volatile bool _hasIndependentRelationships;
#endregion
#region Properties
/// <summary>
/// Returns the kind of the type
/// </summary>
public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.EntitySet; } }
/// <summary>
/// Gets/Sets the entity type of this entity set
/// </summary>
public new EntityType ElementType
{
get
{
return (EntityType)base.ElementType;
}
}
/// <summary>
/// Returns the associations and constraints where "this" EntitySet particpates as the Principal end.
/// From the results of this list, you can retrieve the Dependent IRelatedEnds
/// </summary>
internal ReadOnlyCollection<Tuple<AssociationSet, ReferentialConstraint>> ForeignKeyDependents
{
get
{
if (_foreignKeyDependents == null)
{
InitializeForeignKeyLists();
}
return _foreignKeyDependents;
}
}
/// <summary>
/// Returns the associations and constraints where "this" EntitySet particpates as the Dependent end.
/// From the results of this list, you can retrieve the Principal IRelatedEnds
/// </summary>
internal ReadOnlyCollection<Tuple<AssociationSet, ReferentialConstraint>> ForeignKeyPrincipals
{
get
{
if (_foreignKeyPrincipals == null)
{
InitializeForeignKeyLists();
}
return _foreignKeyPrincipals;
}
}
/// <summary>
/// True if this entity set participates in any foreign key relationships, otherwise false.
/// </summary>
internal bool HasForeignKeyRelationships
{
get
{
if (_foreignKeyPrincipals == null)
{
InitializeForeignKeyLists();
}
return _hasForeignKeyRelationships;
}
}
/// <summary>
/// True if this entity set participates in any independent relationships, otherwise false.
/// </summary>
internal bool HasIndependentRelationships
{
get
{
if (_foreignKeyPrincipals == null)
{
InitializeForeignKeyLists();
}
return _hasIndependentRelationships;
}
}
#endregion
#region Methods
private void InitializeForeignKeyLists()
{
var dependents = new List<Tuple<AssociationSet, ReferentialConstraint>>();
var principals = new List<Tuple<AssociationSet, ReferentialConstraint>>();
bool foundFkRelationship = false;
bool foundIndependentRelationship = false;
foreach (AssociationSet associationSet in MetadataHelper.GetAssociationsForEntitySet(this))
{
if (associationSet.ElementType.IsForeignKey)
{
foundFkRelationship = true;
Debug.Assert(associationSet.ElementType.ReferentialConstraints.Count == 1, "Expected exactly one constraint for FK");
ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints[0];
if (constraint.ToRole.GetEntityType().IsAssignableFrom(this.ElementType) ||
this.ElementType.IsAssignableFrom(constraint.ToRole.GetEntityType()))
{
// Dependents
dependents.Add(new Tuple<AssociationSet, ReferentialConstraint>(associationSet, constraint));
}
if (constraint.FromRole.GetEntityType().IsAssignableFrom(this.ElementType) ||
this.ElementType.IsAssignableFrom(constraint.FromRole.GetEntityType()))
{
// Principals
principals.Add(new Tuple<AssociationSet, ReferentialConstraint>(associationSet, constraint));
}
}
else
{
foundIndependentRelationship = true;
}
}
_hasForeignKeyRelationships = foundFkRelationship;
_hasIndependentRelationships = foundIndependentRelationship;
var readOnlyDependents = dependents.AsReadOnly();
var readOnlyPrincipals = principals.AsReadOnly();
Interlocked.CompareExchange(ref _foreignKeyDependents, readOnlyDependents, null);
Interlocked.CompareExchange(ref _foreignKeyPrincipals, readOnlyPrincipals, null);
}
#endregion
}
}
|