1
我有簡單的一個一對多的關係簡單的一對多流暢,nhiberante
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
public virtual IList<ProductCampaignTargeting> Targetings { get; set; }
}
public class ProductCampaignTargeting
{
public virtual Guid ID { get; set; }
public virtual int TargetType { get; set; }
public virtual string TargetValue { get; set; }
public virtual Product Product { get; set; }
}
與映射:
class ProductCampaignTargetingMap : ClassMap<ProductCampaignTargeting>
{
public ProductCampaignTargetingMap()
{
Table("campaign_targetings");
Id(x => x.ID).GeneratedBy.Guid();
Map(x => x.TargetType).Column("target_type");
Map(x => x.TargetValue).Column("target_value");
References(x => x.Product).Column("campaign_id_fk");
}
}
class ProductMap: ClassMap<Product>
{
public ProductMap()
{
Table("Product");
Id(x => x.Id).Column("id").GeneratedBy.Guid();
Map(x => x.Name).Column("Name");
Map(x => x.Category);
// check why inverse doens't work
HasMany(x => x.Targetings).KeyColumn("campaign_id_fk").Cascade.AllDeleteOrphan().AsBag();
}
}
,它是工作 - 但孩子(很多)表用兩個命令更新 - 插入然後更新 當我想將其更改爲一個命令時,我使用反向()選項 - 但隨後外鍵填充爲空,我在這裏丟失了什麼?
target.Product = product;
product.Targetings.Add(target);
這是原因之一,很多NHibernate的開發者推薦暴露:當您使用逆,因爲你必須添加ProductCampaignTargeting到集合時維持關係的雙方
我認爲一個類似的問題在這裏http://stackoverflow.com/a/1454445/314763回答應該給點您在正確的方向 – frictionlesspulley 2013-04-09 10:57:02
你見過這個問題嗎?http://stackoverflow.com/questions/7935051/unidirectional-parent-child-association-not-null'.Not.KeyNullable()' – Rippo 2013-04-09 11:07:08