[英]Fluent NHibernate Mapping Help static view referencing with compositeID
下午全部。 我正在嘗試為航班段數據庫創建一個映射,其中在映射樹的底部,FlightSegment引用了一個起始和目的地表,該起始和目的地表的CompositeID由三個字母代碼和一個布爾值組成,該布爾值確定該代碼是否為一座城市。
以下是相關的簡化類結構:
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表中分別包含Code和IsCity,且ID參考返回到該航段。
無論我嘗試什么涵義,我都在碰壁。 基本上,我有點混亂。 我已經翻轉了機場和路段之間的關系,只交換了參考。 級聯,而不是級聯。
使用映射時遇到的常見錯誤:
1)UPDATE語句與FOREIGN KEY約束沖突
2){“以下對象的斷開的列映射:GilesSabreConnection.Profiling.Model.BookingEngineModel.Model.GTIFlightSegment的Destination.id,類型component [Code,IsCity]期望2列,但映射了1列”}
3){“外鍵(FK582A9C81E6C3913B:GTIFlightSegment [Destination_id]))的列數必須與引用的主鍵(GTIDestinationAirport [CODE,isCity])相同”}}
萬一需要流利的配置是:
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();
從數據庫和流利的專家那里為我指明正確方向的任何幫助將不勝感激。
提前謝謝了。
意識到通過將GTIAirport映射設置為只讀,我固有地將“目的地”和“原始”映射設置為只讀,因此無論我如何配置關系,該映射都永遠無法工作。
因此,我已將通過名為GTIAiport的視圖訪問的詳細機場信息的靜態參考數據庫分為兩部分,因此不再需要復合鍵中的布爾值。 現在,我可以使用fluent的Component類,將航段映射簡單地記錄在航段表中的機場始發/目的地代碼。 現在,在Airport類中,它本身具有一項功能,可以根據要求使用存儲的Airport代碼從靜態參考數據庫表中獨立獲取豐富數據。
無法找到一種方法來流暢地做到這一點。 有興趣知道是否有辦法。
public GTIFlightSegmentMap()
{
Id(x => x.ID);
Component(x => x.Destination, m =>
{
m.Map(y => y.Code).Column("DestinationCode");
});
Component(x => x.Origin, m =>
{
m.Map(y => y.Code).Column("OriginCode");
});
;
References(x => x.Parent).Not.Nullable();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.