流利的nHibernate Map IList HasMany

时间:2013-10-16 21:07:09

标签: c# nhibernate fluent-nhibernate

我找到很多主题,但我不能在我的测试中做过多次,我有:

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 个答案:

答案 0 :(得分: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");
    }
}