2013-10-16 46 views
0

我找到很多话题,但我不能在我的测试中做的hasMany,我有:功能NHibernate地图的IList的hasMany

public class ProductModel 
{ 
    public virtual Guid Id { get; set; } 
    public virtual string ProductName { get; set; } 
    public virtual IList<LicenseModel> License { get; set; } 

    public ProductModel() 
    { 
     License = new List<LicenseModel>(); 
    } 
} 
public class LicenseModel 
{ 
    public virtual Guid Id { get; set; } 
    public virtual double Price { get; set; } 
    public virtual string Period { get; set; } 
    public virtual int Discount { get; set; } 
    public virtual ProductModel ProductModel { get; set; } 
} 

和映射的一些尝试:以这种方式

public class ProductMap: ClassMap<ProductModel> 
    { 
     public ProductMap() 
     { 
      Id(x => x.Id); 
      Map(x => x.ProductName); 
      HasMany<LicenseModel>(x => x.License) 
       .KeyColumn("Id"); 
      Table("Product"); 
     } 
    } 
public class LicenseMap: ClassMap<LicenseModel> 
{ 
    public LicenseMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.Period); 
     Map(x => x.Price); 
     Map(x => x.Discount); 
     References(x => x.ProductModel) 
      .Class<ProductModel>() 
      .Columns("LicenseId"); 
     Table("License"); 
    } 
} 

我的基地看是这样的:。

表产品看起来不酷:( 一些想法 感谢在咨询

+1

定义“酷”。我不知道你在问什么。一些建议虽然:LicenseId应该是许可证的主要关键。 Id应该变成“ProductId”,你的HasMany地图应该使用它。 –

回答

0

我想这些映射关系将帮助你

public class ProductMap : ClassMap<ProductModel> 
{ 
    public ProductMap() 
    { 
     Table("Product"); 
     Id(x => x.Id); 
     Map(x => x.ProductName); 
     HasMany<LicenseModel>(x => x.License) 
      .Inverse() 
      .KeyColumn("ProductModelId"); 
    } 
} 

public class LicenseMap : ClassMap<LicenseModel> 
{ 
    public LicenseMap() 
    { 
     Table("License"); 
     Id(x => x.Id); 
     Map(x => x.Period); 
     Map(x => x.Price); 
     Map(x => x.Discount); 
     References<ProductModel>(x => x.ProductModel) 
      .Column("ProductModelId"); 
    } 
}