Cascade.All()不删除级联,为什么?

时间:2011-04-22 09:02:09

标签: nhibernate fluent-nhibernate nhibernate-mapping

我需要删除映射在SlotTransceivers和Ports上的EndPoint成员。在过去,EndPoint没有自己的表,这个cklass是SlotTransceiver和Port的一部分。

问题是因为创建Connection,rhitch引用了EndPoints,我必须为EndPoints创建自己的表。有更多的问题,但现在一切正常,除了级联删除EndPoints行。

我相信我需要为EndPoint和Connectiions编写Overriding类,或者为Ports或SlotTransceivers更改此类。

你能给我任何指法吗?我不是原始课程的作者,而且我是Hibernate中的noob。

我认为Cascade.All()意味着当我删除任何SlotTransceiver或Port时,它会自动删除引用的EndPoint。

是否存在新表连接可能引用的问题?

有课程:

public class EndPoint : Entity
    {
        private int _Position;
        private VLAN _VLAN;
        private string _Description;
        private Connection _Connection;

        [Min(0)]
        public virtual int Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }

        [NotNull]
        public virtual VLAN VLAN
        {
            get
            {
                return _VLAN;
            }
            set
            {
                _VLAN = value;
            }
        }

        [Length(500)]
        public virtual string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }
    }

    public class SlotTransceiver : Entity
    {
        private EndPoint _EndPoint;
        private SlotTransceiverItem _InType;
        private Slot _Slot;

        [NotNull]
        public virtual EndPoint EndPoint
        {
            get
            {
                return _EndPoint;
            }
            set
            {
                _EndPoint = value;
            }
        }

        [NotNull]
        public virtual SlotTransceiverItem InType
        {
            get
            {
                return _InType;
            }
            set
            {
                _InType = value;
            }
        }

        [NotNull]
        public virtual Slot Slot
        {
            get
            {
                return _Slot;
            }
        }
    }

    public class Port : Item
    { // Item is inherited from Entity
        private EndPoint _EndPoint;
        [NotNull]
        public virtual EndPoint EndPoint
        {
            get
            {
                return _EndPoint;
            }
            set
            {
                _EndPoint = value;
            }
        }
    }

    public class Connection : Entity
    {
        private EndPoint _EndPointIn;
        private EndPoint _EndPointOut;

        [NotNull]
        public virtual EndPoint EndPointIn
        {

            get
            {
                return _EndPointIn;
            }
            set
            {
                _EndPointIn = value;
            }
        }
        [NotNull]
        public virtual EndPoint EndPointOut
        {

            get
            {
                return _EndPointOut;
            }
            set
            {
                _EndPointOut = value;
            }
        }

    }

它们是一些特殊的映射:

public class SlotTransceiverOverride : IAutoMappingOverride<SlotTransceiver>
    {
        public void Override(FluentNHibernate.Automapping.AutoMapping<SlotTransceiver> mapping)
        {
            // in past EndPoint columns are in SlotTrancievers resp. in Ports tables
            /*
            mapping.Component(x => x.EndPoint, m =>
            {
                m.Map(x => x.Position);
                m.Map(x => x.Description);
                m.References(x => x.VLAN).ForeignKey("SlotTransceiversEndPointVLANFkConstraint");
            });*/
            mapping.References(x => x.InType).Not.LazyLoad();
            mapping.References(x => x.EndPoint).Not.LazyLoad().Cascade.All();
        }
    }

        public void Override(FluentNHibernate.Automapping.AutoMapping<Port> mapping)
        {
            // in past EndPoint columns are in SlotTrancievers resp. in Ports tables
        /*
            mapping.Component(x => x.EndPoint, m =>
            {

                m.Map(x => x.Position);
                m.Map(x => x.Description);
                m.References(x => x.VLAN).ForeignKey("PortsEndPointVLANFkConstraint");
            });
                 */
            mapping.IgnoreProperty(x => x.AbsoluteIndex);
            mapping.References(x => x.EndPoint).Not.LazyLoad();
            mapping.References(x => x.EndPoint).Cascade.All();
        }

1 个答案:

答案 0 :(得分:0)

结合NH属性和一些FNH自动化东西看起来非常可怕。我根本不理解它,但IMO经典ClassMap更加透明。你必须支付更多的击键,但你的头痛更少:)。

public class EndPointMap : ClassMap<EndPoint>
{
    public EndPointMap()
    {
        Id(x => x.Id);
        Map(x => x.Position);
        Map(x => x.Description);
    }
}

public class ConnectionMap : ClassMap<Connection>
{
    public ConnectionMap()
    {
        Id(x => x.Id);

        References(x => x.EndPointIn)
            .Cascade.All()
            .Not.LazyLoad();
        References(x => x.EndPointOut)
            .Cascade.All()
            .Not.LazyLoad();
    }
}

并且此代码也会从数据库中删除连接和端点:

using (var session = OpenSession())
{
    var connection = session.Get<Connection>(connectionId);
    session.Delete(connection);
    session.Flush();
}