Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / ModelProviders / DLinqAssociationProvider.cs / 1305376 / DLinqAssociationProvider.cs
using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data.Linq.Mapping; using System.Linq; using System.Reflection; using System.Diagnostics; using System.Collections.Specialized; using System.Collections; using System.Globalization; namespace System.Web.DynamicData.ModelProviders { internal sealed class DLinqAssociationProvider : AssociationProvider { public DLinqAssociationProvider(DLinqColumnProvider column) { FromColumn = column; MetaAssociation association = column.Member.Association; SetOtherEndOfAssociation(association); SetDirection(association); Debug.Assert(Direction != AssociationDirection.ManyToMany, "Many to Many is not supported by Linq to SQL"); SetAssociationKeyInfo(association); } private void SetAssociationKeyInfo(MetaAssociation association) { DLinqColumnProvider column = (DLinqColumnProvider)FromColumn; ListforeignKeyNames = new List (); int count = column.Member.Association.ThisKey.Count; for (int i = 0; i < count; i++) { MetaDataMember thisKeyMetaDataMember = column.Member.Association.ThisKey[i]; MetaDataMember otherKeyMetaDataMember = column.Member.Association.OtherKey[i]; DLinqColumnProvider thisEntityMemberComponent = FindColumn(column.Table, thisKeyMetaDataMember.Name); if (ShouldRemoveThisAssociation(association)) { column.ShouldRemove = true; return; } foreignKeyNames.Add(thisEntityMemberComponent.Name); if (thisEntityMemberComponent.IsPrimaryKey) { IsPrimaryKeyInThisTable = true; } if (association.IsForeignKey) { thisEntityMemberComponent.IsForeignKeyComponent = true; } } ForeignKeyNames = new ReadOnlyCollection (foreignKeyNames); } private bool ShouldRemoveThisAssociation(MetaAssociation association) { if (Direction == AssociationDirection.ManyToOne && !association.OtherKeyIsPrimaryKey) { return true; } if (Direction == AssociationDirection.OneToMany && !association.ThisKeyIsPrimaryKey) { return true; } if (Direction == AssociationDirection.OneToOne) { if (!association.IsForeignKey && !association.ThisKeyIsPrimaryKey) { return true; } if (association.IsForeignKey && !association.OtherKeyIsPrimaryKey) { return true; } } return false; } private void SetOtherEndOfAssociation(MetaAssociation association) { DLinqTableProvider entityMemberParentEntity = (DLinqTableProvider)FromColumn.Table; DLinqDataModelProvider parentEntityDataContext = (DLinqDataModelProvider)entityMemberParentEntity.DataModel; if (association.OtherMember != null) { ToColumn = parentEntityDataContext.ColumnLookup[(PropertyInfo)association.OtherMember.Member]; } else { ToTable = ((DLinqDataModelProvider)FromColumn.Table.DataModel).DLinqTables.Single(tp => tp.EntityType == association.OtherType.Type); } } private static DLinqColumnProvider FindColumn(TableProvider table, String columnName) { // return (DLinqColumnProvider)table.Columns.First(member => member.Name.Equals(columnName)); } private void SetDirection(MetaAssociation association) { if (association.IsMany) { Direction = AssociationDirection.OneToMany; } else if (association.OtherMember == null || association.OtherMember.Association.IsMany) { // there might not be the other member if this is a one-sided association Direction = AssociationDirection.ManyToOne; } else { Direction = AssociationDirection.OneToOne; } } public override string GetSortExpression(ColumnProvider sortColumn) { return GetSortExpression(sortColumn, "{0}.{1}"); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data.Linq.Mapping; using System.Linq; using System.Reflection; using System.Diagnostics; using System.Collections.Specialized; using System.Collections; using System.Globalization; namespace System.Web.DynamicData.ModelProviders { internal sealed class DLinqAssociationProvider : AssociationProvider { public DLinqAssociationProvider(DLinqColumnProvider column) { FromColumn = column; MetaAssociation association = column.Member.Association; SetOtherEndOfAssociation(association); SetDirection(association); Debug.Assert(Direction != AssociationDirection.ManyToMany, "Many to Many is not supported by Linq to SQL"); SetAssociationKeyInfo(association); } private void SetAssociationKeyInfo(MetaAssociation association) { DLinqColumnProvider column = (DLinqColumnProvider)FromColumn; List foreignKeyNames = new List (); int count = column.Member.Association.ThisKey.Count; for (int i = 0; i < count; i++) { MetaDataMember thisKeyMetaDataMember = column.Member.Association.ThisKey[i]; MetaDataMember otherKeyMetaDataMember = column.Member.Association.OtherKey[i]; DLinqColumnProvider thisEntityMemberComponent = FindColumn(column.Table, thisKeyMetaDataMember.Name); if (ShouldRemoveThisAssociation(association)) { column.ShouldRemove = true; return; } foreignKeyNames.Add(thisEntityMemberComponent.Name); if (thisEntityMemberComponent.IsPrimaryKey) { IsPrimaryKeyInThisTable = true; } if (association.IsForeignKey) { thisEntityMemberComponent.IsForeignKeyComponent = true; } } ForeignKeyNames = new ReadOnlyCollection (foreignKeyNames); } private bool ShouldRemoveThisAssociation(MetaAssociation association) { if (Direction == AssociationDirection.ManyToOne && !association.OtherKeyIsPrimaryKey) { return true; } if (Direction == AssociationDirection.OneToMany && !association.ThisKeyIsPrimaryKey) { return true; } if (Direction == AssociationDirection.OneToOne) { if (!association.IsForeignKey && !association.ThisKeyIsPrimaryKey) { return true; } if (association.IsForeignKey && !association.OtherKeyIsPrimaryKey) { return true; } } return false; } private void SetOtherEndOfAssociation(MetaAssociation association) { DLinqTableProvider entityMemberParentEntity = (DLinqTableProvider)FromColumn.Table; DLinqDataModelProvider parentEntityDataContext = (DLinqDataModelProvider)entityMemberParentEntity.DataModel; if (association.OtherMember != null) { ToColumn = parentEntityDataContext.ColumnLookup[(PropertyInfo)association.OtherMember.Member]; } else { ToTable = ((DLinqDataModelProvider)FromColumn.Table.DataModel).DLinqTables.Single(tp => tp.EntityType == association.OtherType.Type); } } private static DLinqColumnProvider FindColumn(TableProvider table, String columnName) { // return (DLinqColumnProvider)table.Columns.First(member => member.Name.Equals(columnName)); } private void SetDirection(MetaAssociation association) { if (association.IsMany) { Direction = AssociationDirection.OneToMany; } else if (association.OtherMember == null || association.OtherMember.Association.IsMany) { // there might not be the other member if this is a one-sided association Direction = AssociationDirection.ManyToOne; } else { Direction = AssociationDirection.OneToOne; } } public override string GetSortExpression(ColumnProvider sortColumn) { return GetSortExpression(sortColumn, "{0}.{1}"); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- _UriSyntax.cs
- ContextBase.cs
- PopupRootAutomationPeer.cs
- Int16Storage.cs
- OutputCacheModule.cs
- SocketInformation.cs
- InfoCardXmlSerializer.cs
- LinqDataSourceHelper.cs
- ToolboxItemAttribute.cs
- ReadOnlyCollectionBase.cs
- BaseHashHelper.cs
- SafeEventLogReadHandle.cs
- GetReadStreamResult.cs
- ImageCodecInfoPrivate.cs
- ReferentialConstraintRoleElement.cs
- QilUnary.cs
- JsonClassDataContract.cs
- HMACSHA1.cs
- PathTooLongException.cs
- BooleanFunctions.cs
- CustomErrorsSection.cs
- SoapProcessingBehavior.cs
- WindowsSpinner.cs
- ObjectStorage.cs
- ParameterToken.cs
- XamlTreeBuilder.cs
- DynamicDiscoSearcher.cs
- SmiEventStream.cs
- SourceChangedEventArgs.cs
- MatrixKeyFrameCollection.cs
- WebPartEditorCancelVerb.cs
- mda.cs
- SystemParameters.cs
- ConnectionConsumerAttribute.cs
- NodeLabelEditEvent.cs
- DataPagerFieldCollection.cs
- IndexedString.cs
- HotSpotCollection.cs
- securestring.cs
- ViewManager.cs
- OrthographicCamera.cs
- ScriptResourceInfo.cs
- exports.cs
- UInt16Converter.cs
- DoneReceivingAsyncResult.cs
- XmlRootAttribute.cs
- InternalConfigRoot.cs
- CharacterShapingProperties.cs
- InputDevice.cs
- DeferredElementTreeState.cs
- BevelBitmapEffect.cs
- MatrixCamera.cs
- ConcurrencyMode.cs
- DataGridItem.cs
- RolePrincipal.cs
- ProxyWebPartManager.cs
- AxImporter.cs
- JsonWriter.cs
- StreamSecurityUpgradeAcceptor.cs
- InternalsVisibleToAttribute.cs
- BitmapDecoder.cs
- WorkflowRuntimeServiceElement.cs
- DefaultParameterValueAttribute.cs
- KeyEventArgs.cs
- DataGrid.cs
- UIHelper.cs
- UserUseLicenseDictionaryLoader.cs
- ClientBuildManager.cs
- StrongNameUtility.cs
- XamlPointCollectionSerializer.cs
- BindingManagerDataErrorEventArgs.cs
- VisualTarget.cs
- Deflater.cs
- SByte.cs
- TextTreePropertyUndoUnit.cs
- BamlReader.cs
- DrawingBrush.cs
- SizeConverter.cs
- CompilerResults.cs
- GeneralTransform.cs
- RIPEMD160Managed.cs
- _UriSyntax.cs
- IntranetCredentialPolicy.cs
- JavaScriptObjectDeserializer.cs
- ExpandSegmentCollection.cs
- OleDbParameter.cs
- SecurityTokenProvider.cs
- SmiEventSink_Default.cs
- DataGridViewColumnStateChangedEventArgs.cs
- BrowserInteropHelper.cs
- TextFormatter.cs
- FixedSOMPageElement.cs
- XmlChildNodes.cs
- ServiceBusyException.cs
- CalendarDateChangedEventArgs.cs
- CodeMethodInvokeExpression.cs
- DataGridViewButtonColumn.cs
- Localizer.cs
- KnownBoxes.cs
- WriteableBitmap.cs