我们正尝试在应用程序中从NHibernate 3.3更新到NHibernate 4.1,以添加对新数据库系统的支持。不幸的是,与代码映射相关的行为发生了变化,我们无法找到遗漏的内容。
这两个类:
public class Address {
public Address( ) {
this.Id = Guid.NewGuid( );
this.CustomFields = new CustomFields( );
}
public virtual Guid Id { get; set; }
public virtual int Version { get; set; }
public virtual string Notes { get; set; }
public virtual bool Deleted { get; set; }
public virtual string Line1 { get; set; }
public virtual string Line2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zip { get; set; }
public virtual string Country { get; set; }
public virtual ICustomFields CustomFields { get; set; }
}
public class CustomFields {
public CustomFields( ) {
this.Id = Guid.NewGuid( );
this.CustomFieldDictionary = new Dictionary<string, string>( );
}
public virtual Guid Id { get; set; }
public virtual int Version { get; set; }
public virtual string Notes { get; set; }
public virtual bool Deleted { get; set; }
public virtual IDictionary<string, string> CustomFieldDictionary { get; set; }
}
两个映射文件如下所示:
public SQLiteCustomFieldsMap( ) {
this.Table( "CustomFields" );
this.Id( x => x.Id, map => map.Generator( Generators.GuidComb ) );
this.Version( x => x.Version, map => map.Generated( VersionGeneration.Never ) );
this.Property( x => x.Deleted );
this.Property( x => x.Notes, attr => attr.Length( 1000000 ) );
this.Map(
x => x.CustomFieldDictionary,
collectionMapping =>
{
collectionMapping.Table( "CustomFieldItems" );
collectionMapping.Lazy( CollectionLazy.NoLazy );
collectionMapping.Key(
k =>
{
k.Column( "CustomFieldID" );
k.ForeignKey( "ForeignKey_CustomFieldToDictionaryItemID" );
} );
},
mapping => mapping.Element( x => x.Column( "CustomFieldKey" ) ),
elementRelation => elementRelation.Element(
x =>
{
x.Column( "CustomFieldData" );
x.Length( 1000000 );
} ) );
}
和:
public SQLiteAddressMap( ) {
this.Table("Addresses" );
this.Id( x => x.Id, map => map.Generator( Generators.GuidComb ) );
this.Discriminator( x => x.Column("AddressType" ) );
this.DiscriminatorValue("Address" );
this.Version( x => x.Version, map => map.Generated( VersionGeneration.Never ) );
this.Property( x => x.Notes, attr => attr.Length( 1000000 ) );
this.Property( x => x.Deleted );
this.Property( x => x.Line1, attr => attr.Length( 1000 ) );
this.Property( x => x.Line2, attr => attr.Length( 1000 ) );
this.Property( x => x.City, attr => attr.Length( 255 ) );
this.Property( x => x.State, attr => attr.Length( 255 ) );
this.Property( x => x.Zip, attr => attr.Length( 25 ) );
this.Property( x => x.Country, attr => attr.Length( 255 ) );
this.ManyToOne(
x => x.CustomFields,
attr =>
{
attr.Class( typeof(CustomFields) );
attr.ForeignKey( "ForeignKey_AddressToCustomFieldsID" );
attr.Cascade( Cascade.Persist );
} );
}
以下是产生问题的电话:
this._address = new Address
{
Notes = "Notes",
Line1 = "Line 1",
Line2 = "Line 2",
City = "City",
State = "State",
Zip = "Zip",
Country = "Country",
CustomFields = new CustomFields { CustomFieldDictionary = new Dictionary<string, string> { { "Key 1", "Data 1" } } }
};
this.Session.Save( this._address );
在检查日志之后,似乎正在发生的事情是在NHibernate 3.3中,&#39; CustomField&#39;首先插入对象,然后输入&#39;地址&#39;对象,然后是&#39; CustomFieldItems&#39;对象。在NHibernate 4.1中,它试图插入地址&#39;对象优先,它没有通过外键检查,结果测试失败。
谷歌在Stack Overflow上搜索并没有帮助,所以我们现在寻求帮助!我能得到的最接近的是我们必须在ManyToOne()关系中添加一些新属性或更改现有属性,但不确定需要采用哪种方式。感谢所有能帮助我们指明正确方向的人。
编辑:更新了更多信息。