我有這些表/類(例如):功能NHibernate映射問題
table FirstTable (
Id INTEGER NOT NULL DEFAULT AUTOINCREMENT,
Name VARCHAR(100) NOT NULL,
Document VARCHAR(20) NOT NULL
)
table SecondTable (
Id INTEGER NOT NULL,
Something VARCHAR(100) NULL,
FOREIGN KEY (Id) REFERENCES FirstTable (Id)
)
public class FirstClass {
public string Name { get; set; }
public string Document { get; set; }
public SecondClass SecondClass { get; set; }
}
public class SecondClass {
public string Something { get; set; }
public FirstClass FirstClass { get; set; }
}
的映射是:
public class FirstClassMap : ClassMap<FirtsClass> {
Table("FirstTable");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
Map(x => x.Document);
References(x => x.SecondClass, "Id").ForeignKey();
}
public class SecondClassMap : ClassMap<SecondClass> {
Table("SecondTable");
Id(x => x.Id).GeneratedBy.Foreign("FirstClass");
Max(x => x.Something);
HasOne(x => x.FirstClass).PropertyRef(x => x.SecondClass).Cascade.SaveUpdate();
}
的Firstclass可以具有(0,1)二等,和二等可以具有( 1,1)FirstClass。
波紋管代碼返回錯誤「試圖從空一到一個屬性分配ID:二等」
var test = new SecondClass();
test.FirstClass = new FirstClass();
test.Something = "New test";
test.FirstClass.Name = "My name";
test.FirstClass.Document = "My document";
// ... commands to save.
當您反轉實例時它工作嗎?首先實例化FirstClass,然後爲其分配一個新的SecondClass。 – Variant 2011-06-06 22:01:57
嗨@Variant,我試過了,但沒有奏效。韓國社交協會。 – Cesar 2011-06-06 23:24:43