流畅的NHibernate自动映射,简单的问题,为外键关系添加额外的字段

发布于 2024-10-21 16:48:23 字数 3099 浏览 2 评论 0原文

如果我没有任何意义,我很抱歉我的大脑现在已经烧焦了。我刚刚开始在 NHibernate 中编程,由于自动映射,决定转向 Fluent Nhibernate,但我遇到了一些麻烦,这是它应该具有的关系:

在此处输入图像描述

这是 Fluent Nhibernate 自动映射的关系,注意 MemberId 已添加到表中: 在此处输入图像描述

我猜这是因为在我的约定中关系被设置为相反。我需要使关系为 inverse() ,否则级联不起作用。以下是我的约定:

public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "ID");
            //instance.Inverse();
            if (instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false).Length <= 0)
            {
                instance.Cascade.None();
                return;
            }
            var cascadeOption = (CascadeAttribute)instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false)[0];
            switch (cascadeOption.CascadeOption)
            {
                case Enums.CascadeOptions.All:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.AllDeleteOrphan:
                    instance.Cascade.AllDeleteOrphan();
                    break;

                case Enums.CascadeOptions.Delete:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.DeleteOrphan:
                    instance.Cascade.DeleteOrphan();
                    break;


                case Enums.CascadeOptions.None:
                    instance.Cascade.None();
                    break;

                case Enums.CascadeOptions.SaveUpdate:
                    instance.Cascade.SaveUpdate();
                    break;
            }

        }
    }



 public class ReferenceConvention : IReferenceConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
        {
            instance.Column(instance.Property.Name + "Fk");
            if (Attribute.IsDefined(instance.Property.PropertyType.Assembly, typeof(DomainSignatureAttribute)))
                instance.UniqueKey("DomainSignature");
            else
                instance.Index(instance.Property.Name + "Index");
        }
    }

以及我的两个实体:

public class Member : Entity
    {
        public Member()
        {

            Mail = new List<Mail>();
        }


        [Cascade(Enums.CascadeOptions.All)]
        public virtual IList<Mail> Mail
        {
            get; set;
        }



    }


public class Mail : Entity
    {

        public virtual Member Receiver
        {
            get; set;
        }

        public virtual Member Sender
        {
            get; set;
        }

        public virtual string Subject
        {
            get;
            set;
        }

        public virtual string Body
        {
            get; set;
        }
    }

当很明显 Receiver 和 Sender 是成员表的外键时,为什么 Fluent nhibernate 会映射 MemberId?

Im sorry if I don't make any sense my brain is fried right now. I just started programming in NHibernate, decided to move to Fluent Nhibernate because of the automapping but I'm having a little trouble, here is the relation it should have:

enter image description here

Here is the relation Fluent Nhibernate's automapping made, notice that MemberId is added to the table:
enter image description here

I'm guessing this is because the relations are set to inverse in my conventions. I needed to make the relationships inverse() or the cascades didn't work. Here are my conventions:

public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "ID");
            //instance.Inverse();
            if (instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false).Length <= 0)
            {
                instance.Cascade.None();
                return;
            }
            var cascadeOption = (CascadeAttribute)instance.Member.GetCustomAttributes(typeof(CascadeAttribute), false)[0];
            switch (cascadeOption.CascadeOption)
            {
                case Enums.CascadeOptions.All:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.AllDeleteOrphan:
                    instance.Cascade.AllDeleteOrphan();
                    break;

                case Enums.CascadeOptions.Delete:
                    instance.Cascade.All();
                    break;

                case Enums.CascadeOptions.DeleteOrphan:
                    instance.Cascade.DeleteOrphan();
                    break;


                case Enums.CascadeOptions.None:
                    instance.Cascade.None();
                    break;

                case Enums.CascadeOptions.SaveUpdate:
                    instance.Cascade.SaveUpdate();
                    break;
            }

        }
    }



 public class ReferenceConvention : IReferenceConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
        {
            instance.Column(instance.Property.Name + "Fk");
            if (Attribute.IsDefined(instance.Property.PropertyType.Assembly, typeof(DomainSignatureAttribute)))
                instance.UniqueKey("DomainSignature");
            else
                instance.Index(instance.Property.Name + "Index");
        }
    }

And my two entities:

public class Member : Entity
    {
        public Member()
        {

            Mail = new List<Mail>();
        }


        [Cascade(Enums.CascadeOptions.All)]
        public virtual IList<Mail> Mail
        {
            get; set;
        }



    }


public class Mail : Entity
    {

        public virtual Member Receiver
        {
            get; set;
        }

        public virtual Member Sender
        {
            get; set;
        }

        public virtual string Subject
        {
            get;
            set;
        }

        public virtual string Body
        {
            get; set;
        }
    }

Why does fluent nhibernate map MemberId when it's obvious that Receiver and Sender are foreign keys to the member table?

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

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

发布评论

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

评论(1

聽兲甴掵 2024-10-28 16:48:23

因为 Member.Mail 映射为一对多,这会导致 Mail 中存在外键。没有人说过“Member.Mail”是逆关系,也没有人说过外键是如何调用的。因此它创建了一个标准外键。

Because Member.Mail is mapped as one-to-many which results in a foreign key in Mail. Nobody ever said that 'Member.Mail' is an inverse relation and nobody ever said how the foreign key is called. So it creates a standard foreign key.

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