テーブル関係の作成および取得
このトピックでは、テーブルの関連付けを作成、取得する方法を紹介します。
1:N の関連付けの作成
次のコード サンプルでは、EligibleCreateOneToManyRelationship メソッドを使用して、Account
テーブルや Campaign
テーブルが 1:N の関連付けに参加し、CreateOneToManyRequest を使用して関係を作成できることを確認します。
bool eligibleCreateOneToManyRelationship =
EligibleCreateOneToManyRelationship("account", "campaign");
if (eligibleCreateOneToManyRelationship)
{
CreateOneToManyRequest createOneToManyRelationshipRequest =
new CreateOneToManyRequest
{
OneToManyRelationship =
new OneToManyRelationshipMetadata
{
ReferencedEntity = "account",
ReferencingEntity = "campaign",
SchemaName = "new_account_campaign",
AssociatedMenuConfiguration = new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Account", 1033),
Order = 10000
},
CascadeConfiguration = new CascadeConfiguration
{
Assign = CascadeType.NoCascade,
Delete = CascadeType.RemoveLink,
Merge = CascadeType.NoCascade,
Reparent = CascadeType.NoCascade,
Share = CascadeType.NoCascade,
Unshare = CascadeType.NoCascade
}
},
Lookup = new LookupAttributeMetadata
{
SchemaName = "new_parent_accountid",
DisplayName = new Label("Account Lookup", 1033),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Sample Lookup", 1033)
}
};
CreateOneToManyResponse createOneToManyRelationshipResponse =
(CreateOneToManyResponse)service.Execute(
createOneToManyRelationshipRequest);
_oneToManyRelationshipId =
createOneToManyRelationshipResponse.RelationshipId;
_oneToManyRelationshipName =
createOneToManyRelationshipRequest.OneToManyRelationship.SchemaName;
Console.WriteLine(
"The one-to-many relationship has been created between {0} and {1}.",
"account", "campaign");
}
EligibleCreateOneToManyRelationship
次のコード サンプルでは、EligibleCreateOneToManyRelationship
メソッドを使用して、2つのテーブルが 1:N の関連付けに参加できるかどうかを確認するために順番に使用する方法CanBeReferencedRequestそしてCanBeReferencingRequestします。
/// <summary>
/// Determines whether two entities are eligible to participate in a relationship
/// </summary>
/// <param name="referencedEntity">Primary Entity</param>
/// <param name="referencingEntity">Referencing Entity</param>
/// <returns></returns>
public bool EligibleCreateOneToManyRelationship(string referencedEntity,
string referencingEntity)
{
//Checks whether the specified entity can be the primary entity in one-to-many
//relationship.
CanBeReferencedRequest canBeReferencedRequest = new CanBeReferencedRequest
{
EntityName = referencedEntity
};
CanBeReferencedResponse canBeReferencedResponse =
(CanBeReferencedResponse)service.Execute(canBeReferencedRequest);
if (!canBeReferencedResponse.CanBeReferenced)
{
Console.WriteLine(
"Entity {0} can't be the primary entity in this one-to-many relationship",
referencedEntity);
}
//Checks whether the specified entity can be the referencing entity in one-to-many
//relationship.
CanBeReferencingRequest canBereferencingRequest = new CanBeReferencingRequest
{
EntityName = referencingEntity
};
CanBeReferencingResponse canBeReferencingResponse =
(CanBeReferencingResponse)service.Execute(canBereferencingRequest);
if (!canBeReferencingResponse.CanBeReferencing)
{
Console.WriteLine(
"Entity {0} can't be the referencing entity in this one-to-many relationship",
referencingEntity);
}
if (canBeReferencedResponse.CanBeReferenced == true
&& canBeReferencingResponse.CanBeReferencing == true)
{
return true;
}
else
{
return false;
}
}
N:N の関連付けの作成
次のコード サンプルでは、EligibleCreateManyToManyRelationship メソッドを使用して、Account
テーブルや Campaign
テーブルが N:N の関連付けに参加し、CreateManyToManyRequest を使用して関係を作成できることを確認します。
bool accountEligibleParticipate =
EligibleCreateManyToManyRelationship("account");
bool campaignEligibleParticipate =
EligibleCreateManyToManyRelationship("campaign");
if (accountEligibleParticipate && campaignEligibleParticipate)
{
CreateManyToManyRequest createManyToManyRelationshipRequest =
new CreateManyToManyRequest
{
IntersectEntitySchemaName = "new_accounts_campaigns",
ManyToManyRelationship = new ManyToManyRelationshipMetadata
{
SchemaName = "new_accounts_campaigns",
Entity1LogicalName = "account",
Entity1AssociatedMenuConfiguration =
new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Account", 1033),
Order = 10000
},
Entity2LogicalName = "campaign",
Entity2AssociatedMenuConfiguration =
new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Campaign", 1033),
Order = 10000
}
}
};
CreateManyToManyResponse createManytoManyRelationshipResponse =
(CreateManyToManyResponse)service.Execute(
createManyToManyRelationshipRequest);
_manyToManyRelationshipId =
createManytoManyRelationshipResponse.ManyToManyRelationshipId;
_manyToManyRelationshipName =
createManyToManyRelationshipRequest.ManyToManyRelationship.SchemaName;
Console.WriteLine(
"The many-to-many relationship has been created between {0} and {1}.",
"account", "campaign");
}
EligibleCreateManyToManyRelationship
次のコード サンプルでは、CanManyToManyRequest メソッドを使用して、テーブルが N:N の関連付けに参加できるかどうかを確認するために順番に使用する EligibleCreateManyToManyRelationship
メソッドを作成します。
/// <summary>
/// Determines whether the entity can participate in a many-to-many relationship.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns></returns>
public bool EligibleCreateManyToManyRelationship(string entity)
{
CanManyToManyRequest canManyToManyRequest = new CanManyToManyRequest
{
EntityName = entity
};
CanManyToManyResponse canManyToManyResponse =
(CanManyToManyResponse)service.Execute(canManyToManyRequest);
if (!canManyToManyResponse.CanManyToMany)
{
Console.WriteLine(
"Entity {0} can't participate in a many-to-many relationship.",
entity);
}
return canManyToManyResponse.CanManyToMany;
}
テーブル関係の取得
次のコード サンプルは、RetrieveRelationshipRequest を使用して以前に作成された 2 つのテーブル リレーションシップを取得します。 最初の例では MetadataId
を使用し、2 番目の例では Name
を使用します。
//You can use either the Name or the MetadataId of the relationship.
//Retrieve the One-to-many relationship using the MetadataId.
RetrieveRelationshipRequest retrieveOneToManyRequest =
new RetrieveRelationshipRequest { MetadataId = _oneToManyRelationshipId };
RetrieveRelationshipResponse retrieveOneToManyResponse =
(RetrieveRelationshipResponse)service.Execute(retrieveOneToManyRequest);
Console.WriteLine("Retrieved {0} One-to-many relationship by id", retrieveOneToManyResponse.RelationshipMetadata.SchemaName);
//Retrieve the Many-to-many relationship using the Name.
RetrieveRelationshipRequest retrieveManyToManyRequest =
new RetrieveRelationshipRequest { Name = _manyToManyRelationshipName};
RetrieveRelationshipResponse retrieveManyToManyResponse =
(RetrieveRelationshipResponse)service.Execute(retrieveManyToManyRequest);
Console.WriteLine("Retrieved {0} Many-to-Many relationship by Name", retrieveManyToManyResponse.RelationshipMetadata.MetadataId);
関連項目
注意
ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)
この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。