开发者

Fluent NHibernate: Example of a one-to-many relationship on an abstract class of a table-per-subclass implementation

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.

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 cla开发者_C百科ss Fine
{
    public Int64 Cash { get; set; }
}


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> { }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜