Fluent NHibernate:每个子类一个表的实现的抽象类上的一对多关系示例

发布于 2024-09-05 05:22:10 字数 777 浏览 4 评论 0原文

我多年来一直在尝试找到一个示例(因为我自己无法让它工作),以流畅的方式在每个子类实现的抽象类上正确映射一对多关系n休眠。

下面的示例:我希望将 Debt 抽象基类上的 Fines 列表映射到 Fine 类。

如果有人知道他们之前遇到过的任何教程或示例,请告诉我。

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; }
}

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 class Fine
{
    public Int64 Cash { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-09-12 05:22:10

您能告诉我们您到底在哪里遇到困难吗?你尝试过什么?

显然,您需要将所有成员声明为虚拟成员(我认为这是示例中的疏忽)。

但基本上,它看起来像这样:

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

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> { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文