2012-02-17 2 views
1

유창한 nhibernate 자동 매핑을 사용하여 다 대다 관계를 적용하는 데 문제가 있습니다. 위의 코드 그룹 및 수신자는 다 대다 관계를 갖는 것을 설명Fluent Nhibernate Automapping을 사용하는 다 대 다 관계

public class Group 
{ 
    private readonly IList<Recipient> _recipients = new List<Recipient>(); 
    public virtual IList<Recipient> Recipients 
    { 
     get { return _recipients; } 
    } 
} 

public class Recipient 
{ 
    private readonly IList<Group> _groups = new List<Group>(); 
    public virtual IList<Group> Groups 
    { 
     get { return _ groups; } 
    } 
} 

같이 다음

도메인 모델의 단순화 된 형태이다. 우리는 유창한 nhibernate의 자동 매핑 기능을 사용하여 도메인 모델을 데이터베이스와 매핑합니다. 그래서, 우리는 오토메이션에 협약을 사용할 필요가있었습니다. 내가 여기에이 솔루션을 발견

public class ManyToManyConvention : IHasManyToManyConvention 
{ 
    #region IConvention<IManyToManyCollectionInspector,IManyToManyCollectionInstance> Members 

    public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance) 
    { 
     if (instance.OtherSide == null) 
     { 
      instance.Table(
       string.Format(
        "{0}To{1}", 
        instance.EntityType.Name + "_Id", 
        instance.ChildType.Name + "_Id")); 
     } 
     else 
     { 
      instance.Inverse(); 
     } 
     instance.Cascade.All(); 
    } 

    #endregion 
} 

:

http://blog.vuscode.com/malovicn/archive/2009/11/04/fluent-nhibernate-samples-auto-mapping-part-12.aspx#Many%20to%20Many%20convention

을하지만 위의 코드에 Recipients-> 그룹과 모두 시간을 디버깅하는 동안 - : 다음은 우리가 많은 대회에 많은에 사용되는 코드입니다 그룹 ->받는 사람 인스턴스입니다. 기타는 null이 아닙니다. 가정은 첫 번째 인스턴스였습니다 .OtherSide는 null이 아니며 한 번에 관계가 적용될 때 두 번째 null이되므로 역으로 적용 할 것입니다. 그래서 2 개의 매핑 테이블이 동일합니다. 동일한 스키마의 2 개의 테이블을 갖는 것은 데이터베이스에 대한로드입니다. 심지어 많은 관계를 사용하여 데이터베이스에 도메인 모델을 저장하려고 할 때도 마찬가지입니다. 그것은 오직 한 면만 저장합니다. 즉, 수신자를 그룹에 저장하지만 Recipients.In 데이터베이스에 그룹을 저장하지 않는 것입니다. 또한 두 곳 모두에 하나의 매핑 테이블에 항목이 있습니다.

그래서 문제는 올바른 것을하고 있습니까? 그렇지 않다면 어떻게하는지.

+0

당신은 FNH의 버전을 사용 않는 한 그 자체를 반전 걸릴 수 있을까? – Firo

+0

@Firo - It 's 1.2.0.0 – Niraj

답변

2

당신은 기준

public void Apply(IManyToManyCollectionInstance instance) 
    { 
     Debug.Assert(instance.OtherSide != null); 
     // Hack: the cast is nessesary because the compiler tries to take the Method and not the property 
     if (((IManyToManyCollectionInspector)instance.OtherSide).Inverse) 
     { 
      instance.Table(
       string.Format(
        "{0}To{1}", 
        instance.EntityType.Name + "_Id", 
        instance.ChildType.Name + "_Id")); 
     } 
     else 
     { 
      instance.Inverse(); 
     } 
     instance.Cascade.All(); 
    } 
+0

if (instance.OtherSide == null || ((IManyToManyCollectionInspector) instance.OtherSide) .Inverse) – dariol

+0

@ dario-g에서 Assert를 볼 수 있습니까? 'instance.OtherSide == null'은 else에 약간의 추가 생각이 필요합니다. – Firo

관련 문제