使用 Fluent Nhibernate Automapping 建立多对多关系

发布于 2025-01-06 15:41:41 字数 1930 浏览 2 评论 0 原文

我们面临着使用流畅的 nhibernate 自动映射应用多对多关系的问题。

领域模型的简化形式如下:

public class Group
{
    private readonly IList<Recipient> _recipients = new List<Recipient>();
    public virtual IList<Recipient> Recipients
    {
        get { return _recipients; }
    }
}

public class Recipient
{
    private readonly IList<Group> _groups = new List<Group>();
    public virtual IList<Group> Groups
    {
        get { return _ groups; }
    }
}

如上面的代码描述的那样,Group 和 Recipient 是多对多的关系。 我们正在使用 Fluent nhibernate 的自动映射功能来将我们的领域模型与数据库进行映射。因此,我们需要使用约定来进行自动映射。 以下是我们用于多对多约定的代码:-

public class ManyToManyConvention : IHasManyToManyConvention
{
    #region IConvention<IManyToManyCollectionInspector,IManyToManyCollectionInstance> Members

    public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance)
    {
        if (instance.OtherSide == null)
        {
            instance.Table(
               string.Format(
                   "{0}To{1}",
                   instance.EntityType.Name + "_Id",
                   instance.ChildType.Name + "_Id"));
        }
        else
        {
            instance.Inverse();
        }
        instance.Cascade.All();
    }

    #endregion
}

我在这里找到了这个解决方案:

http://blog.vuscode.com/malovicn/archive/2009/11/04/ Fluent-nhibernate-samples-auto-mapping-part-12.aspx#Many%20to%20Many%20convention< /a>

但在上面的代码中,在调试收件人时两次 ->组和组 -> 收件人实例。OtherSide 不为空。假设是第一次实例。OtherSide 将不为空,第二次它将为空,因为关系应用于一侧,因此我们将仅应用相反的关系。 所以它创建了两个相同的映射表。 加载到数据库时有 2 个具有相同架构的表。即使当我尝试使用多对多关系将域模型保存到数据库时也是如此。它只保存一侧,即它保存组中的收件人,但不保存收件人中的组。在数据库中,它也只在一个映射表中包含条目,而不是在两个映射表中都有条目。

所以,问题是我们做的事情正确吗?如果没有的话该怎么办。

We are facing problem applying many-to-many relationship using fluent nhibernate automapping.

The simplified form of domain model are as follows:

public class Group
{
    private readonly IList<Recipient> _recipients = new List<Recipient>();
    public virtual IList<Recipient> Recipients
    {
        get { return _recipients; }
    }
}

public class Recipient
{
    private readonly IList<Group> _groups = new List<Group>();
    public virtual IList<Group> Groups
    {
        get { return _ groups; }
    }
}

As the code above describes that Group and Recipient are having many-to-many relationship.
We are using automapping feature of fluent nhibernate to map our domain model with database. So, we needed to use Convention for automapping.
Following is code we used for many to many convention:-

public class ManyToManyConvention : IHasManyToManyConvention
{
    #region IConvention<IManyToManyCollectionInspector,IManyToManyCollectionInstance> Members

    public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance)
    {
        if (instance.OtherSide == null)
        {
            instance.Table(
               string.Format(
                   "{0}To{1}",
                   instance.EntityType.Name + "_Id",
                   instance.ChildType.Name + "_Id"));
        }
        else
        {
            instance.Inverse();
        }
        instance.Cascade.All();
    }

    #endregion
}

I found this solution here :

http://blog.vuscode.com/malovicn/archive/2009/11/04/fluent-nhibernate-samples-auto-mapping-part-12.aspx#Many%20to%20Many%20convention

But in above code while debugging both time for Recipients-> Groups and for Groups->Recipients instance.OtherSide is coming not null. The assumption was 1st time instance.OtherSide will be not null and second time it will be null as relationship is applied on one side so we will just apply inverse to that.
So it’s creating 2 mapping tables which are same.
It is load to database to have 2 tables of same schema. Even when I try to save our domain model to database using many to many relationship. It’s saving only 1 side i.e it saves Recipients in the Groups , But not saving Groups in Recipients.In database also it is having entry in only one mapping table not in both.

So , the question is Are we doing the right thing? If not then how to do it.

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

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

发布评论

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

评论(1

你怎么敢 2025-01-13 15:41:41

你可以将逆本身作为标准

    public void Apply(IManyToManyCollectionInstance instance)
    {
        Debug.Assert(instance.OtherSide != null);
        // Hack:  the cast is nessesary because the compiler tries to take the Method and not the property
        if (((IManyToManyCollectionInspector)instance.OtherSide).Inverse)
        {
            instance.Table(
               string.Format(
                   "{0}To{1}",
                   instance.EntityType.Name + "_Id",
                   instance.ChildType.Name + "_Id"));
        }
        else
        {
            instance.Inverse();
        }
        instance.Cascade.All();
    }

you could take inverse itself as a criteria

    public void Apply(IManyToManyCollectionInstance instance)
    {
        Debug.Assert(instance.OtherSide != null);
        // Hack:  the cast is nessesary because the compiler tries to take the Method and not the property
        if (((IManyToManyCollectionInspector)instance.OtherSide).Inverse)
        {
            instance.Table(
               string.Format(
                   "{0}To{1}",
                   instance.EntityType.Name + "_Id",
                   instance.ChildType.Name + "_Id"));
        }
        else
        {
            instance.Inverse();
        }
        instance.Cascade.All();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文