2017-02-21 397 views
1
public class Product : EntityBase<Product, int>, IAggregateRoot 
{ 
    public virtual string ProductName { get; set; } 
    public virtual List<ProductDetail> Description { get; set; } 
} 

public class ProductDetail : EntityBase<ProductDetail, int>, IAggregateRoot 
{ 
    public virtual string Description { get; set; } 
    public virtual Product Product { get; set; } 
} 

上述產品實體有多個ProductDetails。我的映射如下:NHibernate一對多映射和保存

public class ProductMap : ClassMapping<Product> 
{ 
    public ProductMap() 
    { 
     Lazy(false); 
     Table("Product"); 

     Id(x => x.ID, map => { map.Column("ID"); map.Generator(Generators.Native); }); 
     Property(x => x.ProductName, map => map.NotNullable(true)); 

     Bag(x => x.Description, m => { 
      m.Inverse(true); // Is collection inverse? 
      m.Cascade(Cascade.All); //set cascade strategy 
      m.Key(k => k.Column(col => col.Name("ProductID"))); //foreign key in Detail table 
     }, a => a.OneToMany()); 
    } 
} 

public class ProductDetailMap : ClassMapping<ProductDetail> 
{ 
    public ProductDetailMap() 
    { 
     Lazy(false); 
     Table("ProductDetail"); 

     Id(x => x.ID, map => { map.Column("ID"); map.Generator(Generators.Native); }); 
     Property(x => x.Description, map => map.NotNullable(false)); 

     ManyToOne(x => x.Product, x => 
     { 
      x.Column("ProductID"); 
     }); 
    } 
} 

當Iam保存這個; Iam得到低於錯誤。

類型的異常「NHibernate.PropertyAccessException」發生在NHibernate.dll但在用戶代碼中沒有處理 附加信息:無效的轉換(檢查您的屬性類型不匹配的映射);這是不是List孩子......這是需要 -

回答

1

NHibernate的將注入其自己IList<>實施

public class Product : EntityBase<Product, int>, IAggregateRoot 
{ 
    public virtual string ProductName { get; set; } 
    //public virtual List<ProductDetail> Description { get; set; } 
    public virtual IList<ProductDetail> Description { get; set; } 
} 
映射集合,我們必須使用接口( IList<>)對於 代理 ...延遲加載