Frage

OK, ist also hier das Problem. Es ist nicht einmal so groß wie der Kerl verrückt, den m will zur Karte: n. Mit unterschiedlichen Spalten zählen in seiner PKs

Egal, was ich tun oder wohin ich scheint aussehen es keine Methode Kette zu sein, die in einer erfolgreichen Zuordnung dieser führen.

Ich habe versucht Verdoppelung auf der Eltern-Kind-Spalten, zB ParentColumn ( „“) ( „) ChildColumn („“) ChildColumn. („“) ParentColumn..“ - nur knapp sein Ziel‘denkt, es funktionieren würde, und ich hatte recht .

habe gerade versucht ForeignKeyConstraintNames kein Glück mit. Noch ist FNH eine Seite mit einer einzigen Tastenbelegung.

        public partial class M2M2ParentAMap : ClassMap<M2M2ParentA>
        {
            public M2M2ParentAMap()
            {
                Table("`M2M2ParentA`");
                Schema("`dbo`");
                CompositeId().KeyProperty( x => x.M2M2ParentAId1, "`M2M2ParentAId1`" ).KeyProperty( x => x.M2M2ParentAId2, "`M2M2ParentAId2`" );
                HasManyToMany(x => x.M2M2ParentB).Schema("`dbo`")
                    .ForeignKeyConstraintNames("FK_M2M2Link_M2M2ParentA", "FK_M2M2Link_M2M2ParentB");
            }
        }



        public partial class M2M2ParentBMap : ClassMap<M2M2ParentB>
        {
            public M2M2ParentBMap()
            {
                Table("`M2M2ParentB`");
                Schema("`dbo`");
                CompositeId().KeyProperty( x => x.M2M2ParentBId1, "`M2M2ParentBId1`" ).KeyProperty( x => x.M2M2ParentBId2, "`M2M2ParentBId2`" );
                HasManyToMany(x => x.M2M2ParentA)
                    .Schema("`dbo`").ForeignKeyConstraintNames("FK_M2M2Link_M2M2ParentB", "FK_M2M2Link_M2M2ParentA");
            }
        }



        public partial class M2M2LinkMap : ClassMap<M2M2Link>
        {
            public M2M2LinkMap()
            {
                Table("`M2M2Link`");
                Schema("`dbo`");
                CompositeId()
                    .KeyProperty( x => x.M2M2ParentA_Id1, "`M2M2ParentA_Id1`" )
                    .KeyProperty( x => x.M2M2ParentA_Id2, "`M2M2ParentA_Id2`" )
                    .KeyProperty( x => x.M2M2ParentB_Id1, "`M2M2ParentB_Id1`" )
                    .KeyProperty( x => x.M2M2ParentB_Id2, "`M2M2ParentB_Id2`" );

                References(x => x.M2M2ParentA).Columns("`M2M2ParentA_Id1`","`M2M2ParentA_Id2`").Cascade.All();
                References(x => x.M2M2ParentB).Columns("`M2M2ParentB_Id1`","`M2M2ParentB_Id2`").Cascade.All();
            }
        }


ERROR:
Foreign key (FK_M2M2Link_M2M2ParentB:M2M2ParentAToM2M2ParentB [M2M2ParentB_id])) must have same number of columns as the referenced primary key (M2M2ParentB [M2M2ParentBId1, M2M2ParentBId2])

und

        public partial class M2M2ParentAMap : ClassMap<M2M2ParentA>
        {
            public M2M2ParentAMap()
            {
                Table("`M2M2ParentA`");
                Schema("`dbo`");
                CompositeId()
                    .KeyProperty( x => x.M2M2ParentAId1, "`M2M2ParentAId1`" )
                        .KeyProperty( x => x.M2M2ParentAId2, "`M2M2ParentAId2`" );

         HasManyToMany(x => x.M2M2ParentB)
            .Schema("`dbo`")
            .Table("`M2M2Link`")
            .ParentKeyColumn("`M2M2ParentA_Id1`")
            .ParentKeyColumn("`M2M2ParentA_Id2`")
            .ChildKeyColumn("`M2M2ParentB_Id1`")
            .ChildKeyColumn("`M2M2ParentB_Id2`");
            }
        }


        public partial class M2M2ParentBMap : ClassMap<M2M2ParentB>
        {
            public M2M2ParentBMap()
            {
                Table("`M2M2ParentB`");
                Schema("`dbo`");
                CompositeId()
                    .KeyProperty( x => x.M2M2ParentBId1, "`M2M2ParentBId1`" )
                    .KeyProperty( x => x.M2M2ParentBId2, "`M2M2ParentBId2`" );

        HasManyToMany(x => x.M2M2ParentA)
            .Schema("`dbo`")
            .Table("`M2M2Link`")
            .ParentKeyColumn("`M2M2ParentB_Id1`")
            .ParentKeyColumn("`M2M2ParentB_Id2`")
            .ChildKeyColumn("`M2M2ParentA_Id1`")
            .ChildKeyColumn("`M2M2ParentA_Id2`");
            }
        }



        public partial class M2M2LinkMap : ClassMap<M2M2Link>
        {
            public M2M2LinkMap()
            {
                Table("`M2M2Link`");
                Schema("`dbo`");
                CompositeId()
                    .KeyProperty( x => x.M2M2ParentA_Id1, "`M2M2ParentA_Id1`" )
                    .KeyProperty( x => x.M2M2ParentA_Id2, "`M2M2ParentA_Id2`" )
                    .KeyProperty( x => x.M2M2ParentB_Id1, "`M2M2ParentB_Id1`" )
                    .KeyProperty( x => x.M2M2ParentB_Id2, "`M2M2ParentB_Id2`" );

                References(x => x.M2M2ParentA)
                    .Columns("`M2M2ParentA_Id1`","`M2M2ParentA_Id2`").Cascade.All();

                References(x => x.M2M2ParentB)
                    .Columns("`M2M2ParentB_Id1`","`M2M2ParentB_Id2`").Cascade.All();
            }
        }

ERROR:
Foreign key (FKAB0E07EA57E45AB6:M2M2Link [M2M2ParentB_Id2])) must have same number of columns as the referenced primary key (M2M2ParentB [M2M2ParentBId1, M2M2ParentBId2])

DDL

CREATE TABLE [dbo].[M2M2ParentA] ( [M2M2ParentAId1] [int] NOT NULL,
                                   [M2M2ParentAId2] [int] NOT NULL,
CONSTRAINT [PK_M2M2ParentA] PRIMARY KEY CLUSTERED ( [M2M2ParentAId1] ASC, [M2M2ParentAId2] ASC ) )

CREATE TABLE [dbo].[M2M2ParentB] ( [M2M2ParentBId1] [int] NOT NULL,
                                   [M2M2ParentBId2] [int] NOT NULL,
CONSTRAINT [PK_M2M2ParentB] PRIMARY KEY CLUSTERED ( [M2M2ParentBId1] ASC, [M2M2ParentBId2] ASC ) )


CREATE TABLE [dbo].[M2M2Link] ( [M2M2ParentA_Id1] [int] NOT NULL,
                                [M2M2ParentA_Id2] [int] NOT NULL,
                                [M2M2ParentB_Id1] [int] NOT NULL,
                                [M2M2ParentB_Id2] [int] NOT NULL,
CONSTRAINT [PK_M2M2Link] PRIMARY KEY CLUSTERED ( [M2M2ParentA_Id1] ASC, [M2M2ParentA_Id2] ASC, [M2M2ParentB_Id1] ASC, [M2M2ParentB_Id2] ASC ) )



ALTER TABLE [dbo].[M2M2Link]
        WITH CHECK
ADD CONSTRAINT [FK_M2M2Link_M2M2ParentA] FOREIGN KEY ( [M2M2ParentA_Id1], [M2M2ParentA_Id2] ) REFERENCES [dbo].[M2M2ParentA] ( [M2M2ParentAId1],
                                                                                                                               [M2M2ParentAId2] )
ALTER TABLE [dbo].[M2M2Link]
        CHECK CONSTRAINT [FK_M2M2Link_M2M2ParentA]
ALTER TABLE [dbo].[M2M2Link]
        WITH CHECK
ADD CONSTRAINT [FK_M2M2Link_M2M2ParentB] FOREIGN KEY ( [M2M2ParentB_Id1], [M2M2ParentB_Id2] ) REFERENCES [dbo].[M2M2ParentB] ( [M2M2ParentBId1],
                                                                                                                               [M2M2ParentBId2] )
ALTER TABLE [dbo].[M2M2Link]
        CHECK CONSTRAINT [FK_M2M2Link_M2M2ParentB]

Update: Ich habe versucht, eine benutzerdefinierten Schlüssel Art zu schaffen, war aber nicht erfolgreich.

Ihre Herausforderung, wenn Sie sich entscheiden, es zu akzeptieren:

Present-Code für das beste Arbeit fließend Mapping diese Tabellenstruktur verwendet wird, wahrscheinlich eine benutzerdefinierte Schlüsseltyp verwendet wird, und die Prämie liegt bei Ihnen.

Wer?

War es hilfreich?

Lösung

Wenn FluentNHibernate zur Zeit nicht in der Lage ist, dies auf der Karte, dann können Sie es mit einer hbm.xml Datei zugeordnet werden.

Ich habe auch eine Komponente für die zusammengesetzte ID beiden Klassen. Dies macht die Identität der Person zu trennen, so dass session.Get<M2M2ParentA>( new M2M2Id( 1, 2 )). Siehe href="https://stackoverflow.com/questions/2301259/hibernate-composite-key/2301332#2301332"> für eine Diskussion über die 3-Wege-Verbund-ID darstellen (es ist das gleiche gilt für NHibernate und Hibernate).

<class name="M2M2ParentA" table="M2M2ParentA">
    <composite-id name="Id" class="M2M2Id">
        <key-property name="Id1" />
        <key-property name="Id2" />
    </composite-id>
    <bag name="BList" table="M2M2Link" lazy="false" fetch="join" >
        <key>
            <column name="M2M2ParentAId1" />
            <column name="M2M2ParentAId2" />
        </key>
        <many-to-many class="M2M2ParentB" >
            <column name="M2M2ParentBId1" />
            <column name="M2M2ParentBId2" />
        </many-to-many>
    </bag>
</class>

<class name="M2M2ParentB" table="M2M2ParentB">
    <composite-id name="Id" class="M2M2Id">
        <key-property name="Id1" />
        <key-property name="Id2" />
    </composite-id>
    <bag name="AList" table="M2M2Link" lazy="false" fetch="join" inverse="true">
        <key>
            <column name="M2M2ParentBId1" />
            <column name="M2M2ParentBId2" />
        </key>
        <many-to-many class="M2M2ParentA" >
            <column name="M2M2ParentAId1" />
            <column name="M2M2ParentAId2" />
        </many-to-many>
    </bag>
</class>

Und meine Version Ihrer Klassen.

public class M2M2ParentA
{
    public M2M2ParentA()
    {
        BList = new List<M2M2ParentB>();
    }
    public virtual M2M2Id Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<M2M2ParentB> BList { get; set; }
}

public class M2M2ParentB
{
    public M2M2ParentB()
    {
        AList = new List<M2M2ParentA>();
    }
    public virtual M2M2Id Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<M2M2ParentA> AList { get; set; }
}

public class M2M2Id
{
    public M2M2Id() {}
    public M2M2Id( int id1, int id2 )
    {
        Id1 = id1;
        Id2 = id2;
    }
    public virtual int Id1 { get; set; }
    public virtual int Id2 { get; set; }
    public override int GetHashCode()
    {
        return Id1.GetHashCode() + Id2.GetHashCode();
    }
    public override bool Equals( object obj )
    {
        M2M2Id other = obj as M2M2Id;
        return other != null && Id1 == other.Id1 && Id2 == other.Id2;
    }
}

Andere Tipps

Die kurze Antwort auf Ihre Frage ist, dass Fluent nicht, dass die Situation noch umgehen kann, und Sie sollen es mit einer .hbm.xml Datei zugeordnet werden.

Die lange Antwort ist, dass Fluent kann es tatsächlich tun, wenn die zusammengesetzten Schlüssel Ihrer 2 übergeordneten Tabellen statt Fremdschlüssel zu 4 separaten Großeltern Tabellen aus (GPA1, GPA2, GPB1, GPB2 - im Beispiel unten). In diesem Fall könnte die Abbildung in etwa so aussehen ... (aber es ist ein bisschen hackey)

//OBJECTS
public class GPA1
{
    public virtual long ID {get;set;}
}
public class GPA2
{
    public virtual long ID { get; set; }
}
public class GPB1
{
    public virtual long ID { get; set; }
}
public class GPB2
{
    public virtual long ID { get; set; }
}
public class M2M2ParentA
{
    public virtual GPA1 ID1A { get; set; }
    public virtual GPA2 ID2A { get; set; }
}
public class M2M2ParentB
{
    public virtual GPB1 ID1B { get; set; }
    public virtual GPB2 ID2B { get; set; }
}
public class M2M2Link
{
    public virtual M2M2ParentA LinkA { get; set; }
    public virtual M2M2ParentB LinkB { get; set; }

    public virtual GPA1 ID1A
    {
        get { return LinkA.ID1A; }
        set { LinkA.ID1A = value; }
    }
    public virtual GPA2 ID2A
    {
        get { return LinkA.ID2A; }
        set { LinkA.ID2A = value; }
    }
    public virtual GPB1 ID1B
    {
        get { return LinkB.ID1B; }
        set { LinkB.ID1B = value; }
    }
    public virtual GPB2 ID2B
    {
        get { return LinkB.ID2B; }
        set { LinkB.ID2B = value; }
    }
}
//FLUENT MAPPINGS
public class GPA1Map : ClassMap<GPA1>
{
    public GPA1Map()
    {
        Table("GPA1");

        Id(x => x.ID, "id_column");
    }
}
public class GPA2Map : ClassMap<GPA2>
{
    public GPA2Map()
    {
        Table("GPA1");

        Id(x => x.ID, "id_column");
    }
}
public class GPB1Map : ClassMap<GPB1>
{
    public GPB1Map()
    {
        Table("GPA1");

        Id(x => x.ID, "id_column");
    }
}
public class GPB2Map : ClassMap<GPB2>
{
    public GPB2Map()
    {
        Table("GPA1");

        Id(x => x.ID, "id_column");
    }
}
public class M2M2ParentAMap : ClassMap<M2M2ParentA>
{
    public M2M2ParentAMap()
    {
        Table("M2M2ParentA");

        CompositeId()
            .KeyReference(x => x.ID1A, "M2M2ParentAId1")
            .KeyReference(x => x.ID1A, "M2M2ParentAId2");
    }
}
public class M2M2ParentBMap : ClassMap<M2M2ParentB>
{
    public M2M2ParentBMap()
    {
        Table("M2M2ParentB");

        CompositeId()
            .KeyReference(x => x.ID1B, "M2M2ParentBId1")
            .KeyReference(x => x.ID1B, "M2M2ParentBId2");
    }
}
public class M2M2LinkMap : ClassMap<M2M2Link>
{
    public M2M2LinkMap()
    {
        Table("M2M2Link");

        CompositeId()
            .KeyReference(x => x.ID1A, "M2M2ParentA_Id1")
            .KeyReference(x => x.ID1B, "M2M2ParentA_Id2")
            .KeyReference(x => x.ID2A, "M2M2ParentB_Id1")
            .KeyReference(x => x.ID2B, "M2M2ParentB_Id2");
    }
}

Es sollte in jedem dieser Mapping-Klassen HasMany <> Beziehungen sein, aber ich habe faul. Wenn Sie die Route der einschließlich einer .hbm.xml Datei gehen, würde ich empfehlen die .ExportTo mit ( „c: \“) Funktion, wenn Sie Ihre ISessionFactory konfigurieren und Sie können nur die hbm Datei, die fließend puts out bearbeiten. Es ist ein guter Ausgangspunkt.

HTH, -Danno

scroll top