我有一个db模式:
产品
ProductVariation
可以预见,类看起来有点像这样:
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(在数据库上)
答案 0 :(得分:0)
public class Product
{
public virtual int ID { get; set; }
public virtual string StoreBrand { get; set; }
public virtual string ProductName { get; set; }
public virtual string Description { get; set; }
public virtual IEnumerable<ProductVariation> Variations { get; set; }
public override Equals(object obj)
{
return Equals(obj as Product)
}
public override Equals(Product other)
{
return (other != null) && (Id == other.Id) && (StoreBrand == other.StoreBrand);
}
public override GetHashCode()
{
unchecked
{
return Id.GetHashCode() * 397 + StoreBrand.GetHashCode();
}
}
}
public class ProductVariation
{
public virtual int ID { 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 alone is not unique, hence compositeId
CompositeId()
.KeyProperty(x => x.ID)
.KeyProperty(x => x.StoreBrand);
Map(x => x.ProductName);
Map(x => x.Description);
HasMany(x => x.Variations)
.KeyColumn("ProductID", "StoreBrand");
}
}
public class ProductVariationMapper : ClassMap<ProductVariation>
{
public ProductVariation()
{
Id(x => x.ID);
Map(x => x.Size);
Map(x => x.Price);
References(x => x.Product)
.Column("ProductID", "StoreBrand");
}
}