简体   繁体   English

流利的nHibernate Map IList HasMany

[英]Fluent nHibernate Map IList HasMany

I find many topics but i cant do hasMany in my tests, i have: 我发现很多主题,但是在测试中我做不到很多,我有:

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; }
}

And some try of mapping: 和一些映射尝试:

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");
    }
}

in that way my base look like this: 这样,我的基础如下所示:

Table Product don't look cool :( Some ideas? Thank in Advice. 表产品看起来不酷:(有什么想法?谢谢您的建议。

I think these mappings will help you 我认为这些映射将对您有所帮助

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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM