映射产品和购物车-NHibernate
我试着用我的手去弄NHibernate和流利的NHibernate。我写了两门课如下:映射产品和购物车-NHibernate,nhibernate,fluent-nhibernate,fluent-nhibernate-mapping,Nhibernate,Fluent Nhibernate,Fluent Nhibernate Mapping,我试着用我的手去弄NHibernate和流利的NHibernate。我写了两门课如下: public class Product { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual decimal Price { get; set; } } public class ShoppingCart { public virtual i
public class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual decimal Price { get; set; }
}
public class ShoppingCart
{
public virtual int Id { get; set; }
public IList<Product> Products { get; set; }
public decimal CartTotal
{
get { return Products.Aggregate(0m, (c,x)=> c + x.Price; ); }
}
public ShoppingCart()
{
Products = new List<Product>();
}
}
公共类产品
{
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟十进制价格{get;set;}
}
公共类购物车
{
公共虚拟整数Id{get;set;}
公共IList产品{get;set;}
公共十进位总计
{
获取{返回产品.聚合(0m,(c,x)=>c+x.价格;);}
}
公共购物车()
{
产品=新列表();
}
}
我想映射Product
和ShoppingCart
,但我不希望任何ShoppingCart.Id
作为Products
表中的键。如何使用Fluent NHibernate
定义地图
PS:-我尝试使用自引用类别映射
类别
和成功类别
。但我无法解决购物车
和产品
问题。另外,我想使用mssqlserverce4.0
我认为,您需要多对多关系。这可以通过以下映射在Fluent中完成:
public class ShoppingCartMap : ClassMap<ShoppingCart>
{
public ShoppingCartMap()
{
HasManyToMany(x => x.Products).Table("ShoppingCartToProduct");
// Other properties follow...
}
}
公共类ShoppingCartMap:ClassMap
{
公共购物车地图()
{
HasManyToMany(x=>x.Products).Table(“ShoppingCartToProduct”);
//其他属性如下。。。
}
}
这将生成一个名为“ShoppingCartToProduct”的表,其中包含两个外键列(一个到Product.Id,一个到ShoppingCart.Id)。我认为,您需要多对多关系。这可以通过以下映射在Fluent中完成:
public class ShoppingCartMap : ClassMap<ShoppingCart>
{
public ShoppingCartMap()
{
HasManyToMany(x => x.Products).Table("ShoppingCartToProduct");
// Other properties follow...
}
}
公共类ShoppingCartMap:ClassMap
{
公共购物车地图()
{
HasManyToMany(x=>x.Products).Table(“ShoppingCartToProduct”);
//其他属性如下。。。
}
}
这将生成一个名为“ShoppingCartToProduct”的表,其中包含两个外键列(一个到Product.Id,一个到ShoppingCart.Id)