Fluent NHibernate Mapping使用compositeID

时间:2016-02-24 16:17:10

标签: c# fluent-nhibernate fluent-nhibernate-mapping

下午全部。我试图为飞行段数据库创建一个映射,在映射树的底部,FlightSegment引用一个原始目标表和一个目标表,复合ID由一个三字母代码和一个确定代码是否为和#39;塔城。

以下是相关的简化类结构:

    public class GTIFlightSegment
        {
            public virtual int ID { get; protected set; }


            public virtual GTIOriginAirport Origin { get; set; }
            public virtual GTIDestinationAirport Destination { get; set; }

            public virtual GTIFlightSegmentGroup Parent { get; set; }

        }

  public class GTIAirport
    {

        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual  string City { get; set; }
        public virtual string CountryCode { get; set; }
        public virtual GTIGeoCode GeoCode {get; set; }
        public virtual string Terminal { get; set; }
        public virtual bool IsCity { get; set; }
        public GTIAirport()
        {
            GeoCode = new GTIGeoCode();
            IsCity = false;
        }

        public override bool Equals(object obj)
        {
            var other = obj as GTIAirport;

            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return this.Code == other.Code && this.IsCity == other.IsCity;
        }


        public override int GetHashCode()
        {
            unchecked
            {
                int hash = GetType().GetHashCode();
                hash = (hash * 31) ^ Code.GetHashCode();
                hash = (hash * 31) ^ IsCity.GetHashCode();

                return hash;
            }
        }

    }

    public class GTIOriginAirport : GTIAirport
    {
        public virtual GTIFlightSegment Parent { get; set; }

        public GTIOriginAirport() : base()
        {

        }
    }

    public class GTIDestinationAirport : GTIAirport
    {

        public virtual GTIFlightSegment Parent { get; set; }

        public GTIDestinationAirport() : base()
        {

        }
    }

以下是我目前为对象创建的映射:

public class GTIFlightSegmentMap : ClassMap<GTIFlightSegment>
    {
        public GTIFlightSegmentMap()
        {
            Id(x => x.ID);

            References(x => x.Destination).Columns(new string[] { "DestinationCODE", "DestinationIsCity" }).Cascade.All();
            References(x => x.Origin).Columns(new string[] { "OriginCODE", "OriginIsCity"}).Cascade.All();

            References(x => x.Parent).Not.Nullable();


        }
    }


 public class GTIAirportMap : ClassMap<GTIAirport>
    {
        public GTIAirportMap()
        {
            Table("GTIAirport");
            ReadOnly();
            CompositeId()
                .KeyProperty(x => x.Code, "CODE")
                .KeyProperty(x => x.IsCity, "isCity");                 

            Map(x => x.Name).Column("Airport");
            Map(x => x.City);
            Map(x => x.CountryCode);
            Component(x => x.GeoCode, m =>
            {
                m.Map(x => x.Latitude);
                m.Map(x => x.Longitude);

            });

        }

    }

    public class GTIOriginAirportMap : SubclassMap<GTIOriginAirport>
    {
        public GTIOriginAirportMap()
        {
            KeyColumn("CODE");
            KeyColumn("isCity");
            HasOne(x => x.Parent).PropertyRef(x => x.Origin);
        }
    }


    public class GTIDestinationAirportMap : SubclassMap<GTIDestinationAirport>
    {
        public GTIDestinationAirportMap()
        {
            KeyColumn("CODE");
            KeyColumn("isCity");
            HasOne(x => x.Parent).PropertyRef(x => x.Origin);
        }
    }

我想要实现的是,当在系统中创建FlightSegment时,它会将DestinationIsCity / DestinationCode和OriginIsCity / OriginCode存储在航段段表中,但不会尝试更新GTIAirport参考表视图。但是,当通过查询从数据库中检索对象时,将获取GTIAirport参考表中的丰富信息。

或者可能在DepartureAirport和OriginAirport表中分别使用代码和IsCity,并将ID引用返回到航段。

无论我尝试什么样的内涵,我都会碰到某种类型的墙壁。基本上我让自己陷入了混乱。我已经翻转了机场与细分市场之间的关系,只交换了参考资料。级联,不级联。

使用映射时遇到的常见错误:

1)UPDATE语句与FOREIGN KEY约束冲突

2){&#34;断列映射:Destination.id:GilesSabreConnection.Profiling.Model.BookingEngineModel.Model.GTIFlightSegment,类型组件[Code,IsCity]需要2列,但是映射了1列#34; }

3){&#34;外键(FK582A9C81E6C3913B:GTIFlightSegment [Destination_id]))必须与引用的主键具有相同的列数(GTIDestinationAirport [CODE,isCity])&#34;}

如果需要,流畅的配置是:

return Fluently.Configure().Database(MsSqlConfiguration.MsSql2012.ConnectionString(@"Data Source=Dev2;Initial Catalog=Sandbox;Integrated Security=True")).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Profile>()).ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true)).BuildSessionFactory();

任何帮助指出我从数据库和流利的大师正确的方向将非常感谢。

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

意识到通过将GTIAirport Map设置为readonly,我本来就是将Destination和Origin地图设置为只读,所以无论我如何配置关系,映射都无法工作。

因此,我将通过名为GTIAiport的View访问的详细机场信息的静态参考数据库拆分为两个,因此不再需要复合键中的布尔值。我现在有了航程段映射,只需使用流畅的组件类在航班段表中记录机场起点/目的地代码。在机场类本身现在有一个功能,可以根据请求使用存储的机场代码独立地从静态参考数据库表中获取丰富的数据。

无法弄清楚如何用流利的方式做到这一点。有兴趣知道是否有办法。

ActiveX component can't create object: 'QuickTest.Application'.
Line (2): "Set qtApp =CreateObject("QuickTest.Application","172.16.136.103")"