Fluent NHibernate - 使用 2 列映射一对多
我有一个数据库架构,大致如下:
Product
- ID
- ProductName
- Description
- StoreBrand
ProductVariation
- VariationID
- ProductID
- Size
- StoreBrand
- Price
Classes,可以预见,看起来有点像这样:
public class Product
{
public virtual int ID { get; set; }
public virtual string ProductName { get; set; }
public virtual string Description { get; set; }
public virtual string StoreBrand { get; set; }
public virtual IEnumerable<ProductVariation> Variations { get; set; }
}
public class ProductVariation
{
public virtual int VariationID { get; set; }
public virtual int ProductID { get; set; }
public virtual Product Product {get; set;}
public virtual string Size { get; set; }
public virtual double Price { get; set; }
}
我有像这样映射类:
public class ProductMapper : ClassMap<Product>
{
public ProductMapper()
{
Id(x => x.ID);
Map(x => x.ProductName);
Map(x => x.Description);
Map(x => x.StoreBrand);
HasMany(x => x.Variations)
.KeyColumn("ProductID");
}
}
public class ProductVariationMapper : ClassMap<ProductVariation>
{
public ProductVariation()
{
Id(x => x.ID);
Map(x => x.ProductID);
Map(x => x.Size);
Map(x => x.Price);
References(x => x.Product)
.Column("ProductID");
}
}
这有点工作...
但是,我需要做的是将 Product.Brands 与 ProductVariation.Brands 也绑定在一起...(反之亦然)
所以查询产品,返回该品牌的 ProductVariations 列表... (请注意,ProductVariation 在类中没有属性,但它具有用于映射的列)
ProductVariation.ID 不是唯一的。 关键是 ProductVariation.ID 和 ProductVariation.Brand (在数据库上)
I have a db schema along the lines of:
Product
- ID
- ProductName
- Description
- StoreBrand
ProductVariation
- VariationID
- ProductID
- Size
- StoreBrand
- Price
Classes, predictably, look a bit like this:
public class Product
{
public virtual int ID { get; set; }
public virtual string ProductName { get; set; }
public virtual string Description { get; set; }
public virtual string StoreBrand { get; set; }
public virtual IEnumerable<ProductVariation> Variations { get; set; }
}
public class ProductVariation
{
public virtual int VariationID { get; set; }
public virtual int ProductID { get; set; }
public virtual Product Product {get; set;}
public virtual string Size { get; set; }
public virtual double Price { get; set; }
}
I've got the mapping classes like this:
public class ProductMapper : ClassMap<Product>
{
public ProductMapper()
{
Id(x => x.ID);
Map(x => x.ProductName);
Map(x => x.Description);
Map(x => x.StoreBrand);
HasMany(x => x.Variations)
.KeyColumn("ProductID");
}
}
public class ProductVariationMapper : ClassMap<ProductVariation>
{
public ProductVariation()
{
Id(x => x.ID);
Map(x => x.ProductID);
Map(x => x.Size);
Map(x => x.Price);
References(x => x.Product)
.Column("ProductID");
}
}
This is kind of working...
However, what I need to do is tie the Product.Brands together with the ProductVariation.Brands as well... (and vice versa)
So querying Product, returns a list of it's ProductVariations for that brand...
(Notice, ProductVariation doesn't have a property in the class, but it has the column for mapping)
ProductVariation.ID is non unique.
The key is ProductVariation.ID and ProductVariation.Brand (on the database)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)