2015-01-09 2 views
1

특별한 경우 외래 키 관계에서 일부 CascadeOnDelete 표시를 제거하려고합니다.CascadeOnDelete를 삭제하는 협약

관계의 한쪽 끝이 특정 유형이고 다른 쪽 끝이 아닌 경우 cascadeOnDelete를 false로 설정하려고합니다.

class CascadeOnDeleteSuppressionConvention : IConceptualModelConvention<AssociationType>, IConvention 
{ 
    public void Apply(AssociationType associationType, DbModel model) 
    { 
    if(!associationType.IsForeignKey) 
     return; 

    if(associationType.AssociationEndMembers[0].GetPOCOType() == typeof(someType) && 
     associationType.AssociationEndMembers[1].GetPOCOType() != typeof(someTypeOtherType)) 
     associationType.AssociationEndMembers[0].DeleteAction = DeleteAction.None; 
    } 
} 

유감스럽게도 코드 첫 번째 모델에서 POCO 유형을 얻는 데는 단서가 없습니다.
누군가가 그 유형을 얻는 방법에 대한 정보를 제공 할 수 있습니까?

답변

1

개념적 모델의 EntityType과 응용 프로그램의 CLRType 간의 매핑을 얻는 해결책을 찾았습니다. 냈다 그 코드가 필요한 정보를 얻고 EntityType 일치 여부를 확인하는 데 사용할 수 있습니다

public EntityType FindEntityType(DbModel model, Type type) 
{ 
    var const metadataPropertyName = "http://schemas.microsoft.com/ado/2013/11/edm/customannotation:ClrType"; 

    var entityType = model.ConceptualModel.EntityTypes.SingleOrDefault(
     e => e.MetadataProperties.Contains(metadataPropertyName) && 
      e.MetadataProperties.GetValue(metadataPropertyName).Value as Type == type 
     ); 

    return entityType; 
} 

다음 ConceptualModel.EntityTypes 내부

여기 내 요구에 맞는 가능한 메타 데이터입니다.

ClrType 코드에 EntityType

public Type GetClrType(EntityType entityType) 
{ 
    const string metadataPropertyName = "http://schemas.microsoft.com/ado/2013/11/edm/customannotation:ClrType"; 

    MetadataProperty metadataProperty; 
    if (entityType.MetadataProperties.TryGetValue(metadataPropertyName, true, out metadataProperty)) 
     return metadataProperty.Value as Type; 

    return null; 
}