Frage

I am trying to map two tables :

  • the first one has an id (idA) and a field with the id (idB) of the other table.
    • the other one has a composite key based on the previous id (idB) and an other one (idB2)

The idea is that the second table contains the description of A split on multiple rows.

My current implementation is the following but I am unable to retrieve the partialDescription needed for concatenation. How should I change my mapping to work? Any ideas? :)

public class A
{
    long idA
    lond idB
    string fullDescription
}


public class B
{
    long idB
    long idB2
    strind partialDescription
}   

public class AMap : ClassMap<A>
{
    public AMap()
    {
        Table("A");
        Id(x => x.id).Column("idA").GeneratedBy.Native();
        Map(x => x.idB).Column("idB")
        HasMany(x => x.B).KeyColumn("idB").Inverse().Cascade.All().Not.LazyLoad();
    }
}

 public class BMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");
        CompositeId()
            .KeyReference(x => x.A, "idB")
            .KeyProperty(x => x.idB2, "idB2");
        Map(x => x.partialDescription, "desc").CustomType("AnsiString");
    }
}
War es hilfreich?

Lösung

In fact, by your description, you must have this another scenario:

public class A
{   
    public virtual long Id { get; set; }
    public virtual IList<B> PartialDescriptions { get; protected set; }
    public string fullDescription
    {
        get
        {
            StringBuilder description = new StringBuilder();
            foreach (var partial in PartialDescriptions)
            {
                description.Append(partial);
            }

            return description.ToString();
        }
    }
}

public class B
{
    public virtual long Id { get; set; }
    public virtual long Id2 { get; set; }
    public virtual string Description { get; set; }
}

Then, try to implement your class maps like this:

public class BMap : ClassMap<B>
{
    public BMap()
    {
        Table("B");
        CompositeId()
            .KeyReference(x => x.Id, "idB")
            .KeyProperty(x => x.Id2, "idB2");
        Map(x => x.partialDescription, "desc").CustomType("AnsiString");
    }
}

public class AMap : ClassMap<A>
{
    public AMap()
    {
        Table("A");
        Id(x => x.Dd).Column("idA").GeneratedBy.Native();
        HasMany(x => x.PartialDescriptions)
            .KeyColumn("idB")
            .Inverse()
            .Cascade.All()
            .Not.LazyLoad();
    }
}

NOTE: I didn't tried to compile this code. I only expect that you can take the general .

I recommend you that take a look into Getting started section of Fluent NHibernate to more information.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top