为什么我的Fluent NHibernate SubClass Mappings会生成冗余列?

时间:2010-12-25 22:18:30

标签: c# .net nhibernate fluent-nhibernate

我有以下实体

public abstract class Card
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }

    public virtual Product Product { get; set; }
    public virtual Sprint Sprint { get; set; }
}
public class Story:Card
{
    public virtual double Points { get; set; }
    public virtual int Priority { get; set; }
}
public class Product
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Story> Stories { get; private set; }

    public Product()
    {
         Stories = new List<Story>();
    }
}

以下映射

public class CardMap:ClassMap<Card>
{
    public CardMap()
    {
        Id(c => c.Id)
            .Index("Card_Id");

        Map(c => c.Name)
            .Length(50)
            .Not.Nullable();
        Map(c => c.Description)
            .Length(1024)
            .Not.Nullable();

        References(c=>c.Product)
            .Not.Nullable();

        References(c=>c.Sprint)
            .Nullable();

    }
}
public class StoryMap : SubclassMap<Story>
{
    public StoryMap()
    {
        Map(s => s.Points);
        Map(s => s.Priority);

    }
}
public class ProductMap:ClassMap<Product>
{
    public ProductMap()
    {
        Id(p => p.Id)
            .Index("Product_Id");

        Map(p => p.Name)
            .Length(50)
            .Not.Nullable();

        HasMany(p => p.Stories)
            .Inverse();
    }

当我生成Schema时,表格按如下方式创建

Card
---------
Id
Name
Description
Product_id
Sprint_id

Story
------------
Card_id
Points
Priority
Product_id
Sprint_id

我原本期望看到Card表中的Product_id和Sprint_id列,而不是Story表。

我做错了什么或误解了什么?

2 个答案:

答案 0 :(得分:1)

注意:仅在NH2项目上进行测试

嗯,一旦你读到这个,你可能会想要在门上咀嚼,但TLDR的原因是因为Product_id表中的Spring_idStory列是< strong> not 冗余 - 它们存在于SpringMap和ProductMap中的HasMany(x => x.Stories)关系中。它们恰好与CardMap References(x => x.ProductReferences(x => x.Sprint)共享相同的命名约定。

通过评论ProductMap.cs:24-25SprintMap.cs:22并重建来验证这一点。

如果以上内容没有意义,请告诉我,我会尝试进一步详细说明。

所以,应该正常工作。如果您想澄清列,您可以明确定义列名称,如下所示:

ProductMap.cs
        HasMany(p => p.Stories)
            .KeyColumn("ProductOwner_id")
            .Inverse();

SprintMap.cs
        HasMany(s => s.Stories)
            .KeyColumn("SprintOwner_id")
            ;

CardMap.cs
        References(c=>c.Product)
            .Column("Product_id")
            .Not.Nullable();

        References(c=>c.Sprint)
            .Column("Sprint_id")
            .Nullable();

这里我猜测故事和产品/ Sprint之间的1:N关系是“所有者”。您可能希望将其重命名为适合语义的任何内容。

另一件事。 会认为最后的更改(对CardMap.cs的更改)是不必要的 - 但它们似乎是出于某种原因,或者Sprint_id列变为{{1} }。我不知道为什么会发生这种情况 - 我会推测这是某种双向关系推理,对于流利/ nhibernates部分出错了,但我只花了很少钱。

答案 1 :(得分:0)

我看到Story实体继承自您创建的Card实体,但您不知道为什么在Story表架构中有Product_Id和Sprint_Id属性,因为它们是虚拟属性在卡类中。

我猜这是因为在NHibernate中,所有属性都需要是虚拟的,但最初只需要。他们并没有真正保持虚拟。 NHibernate框架会覆盖它们,可能正因为如此,这种情况发生在你身上。

相关问题