views:

55

answers:

1

Hi All,

I've been trying for ages to find an example (because I can't get it to work myself) of the correct mapping for a one-to-many relationship on an abstract class of a table-per-subclass implementation, in fluent nHibernate.

An example below: I'm looking to map the list of Fines on the Debt abstract base class to the Fine class.

if anyone knows of any tutorial or example they've come across before please let me know.

Thanks, Tim

public abstract class Entity
{
    public int Id { get; set; }
}

public abstract class Debt : Entity
{        
    public decimal Balance { get; set; }

    public IList<Fine> Fines { get; set; }

    public Debt()
    {
        Fines = new List<Fine>();
    }

}

public class CarLoan : Debt
{

}

public class CreditCard : Debt
{

}

public class LoanApplication : Entity
{
    public IList<Debt> ExistingDebts { get; set; }

    public LoanApplication()
    {
        ExistingDebts = new List<Debt>();
    }
}

public class Fine
{
    public Int64 Cash { get; set; }
}
A: 

Can you tell us where exactly you're having difficulty? What have you tried?

Obviously, you'll need to declare all of your members as virtual (I assume this was an oversight in the example).

Basically, though, it would look like this:

public DebtMap : ClassMap<Debt>
{
    public DebtMap()
    {
        Id(x => x.Id);
        HasMany(x => x.Fines);
    }
}

public FineMap : ClassMap<Fine>
{
    public FineMap()
    {
        Id(x => x.Id);
        // map other members
    }
}

public CarLoanMap : SubclassMap<CarLoan> { }
public CreditCardMap : SubclassMap<CreditCard> { }
Jay
Thanks for your help, but I was really just looking for an example/tutorial where I can see it working.The above is just a contrived example of what I'm looking to do. This is for work and is quite a complex piece of work which is all working fine apart from the one-to-many on the abstract base (all the virtuals are there etc). We're using the automapper and overrides where necessary.So trying to explain exactly what we've done in terms of configuration, mapping overrides etc. would make thigs difficult to follow.Which is why I was just wondering if anyone knew of any examples etc.Tim
BigTommy79