我想在产品和购物车之间映射一个M:N联合物,其中包括与其相关的产品和产品。但数据并不持久,但我不知道你为什么能帮助我?
以下是CartControler的代码
Create();
Product productD = new ProductDao().GetById(product);
ProductsOfCart productsOfCart = new ProductsOfCart();
User user = new UserDao().GetByLogin(User.Identity.Name);
Cart cart = new CartDao().GetByUser(user);
cart.Price += productD.Price;
cart.PriceDph += productD.PriceDph;
cart.NumberOfItems++;
ProductsOfCartDao productsOfCartDao = new ProductsOfCartDao();
CartDao cartDao = new CartDao();
CartDao.Update(cart);
productsOfCart.IdCart = cart;
productsOfCart.IdCart = productD;
productsOfCartDao.Create(productsOfCart);
购物车,产品和产品OverCart
public class Cart :IEntity
{
public virtual int Id { get; set; }
public virtual double Price { get; set; }
public virtual double PriceDph { get; set; }
public virtual int NumberOfItems { get; set; }
public virtual User IdUser { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int Price { get; set; }
public virtual string ProductState { get; set; }
public virtual string Maker { get; set; }
public virtual string Description { get; set; }
public virtual int ProductWaranty { get; set; }
public virtual ProductCategory Category { get; set; }
public virtual string ImageName { get; set; }
public virtual IList<Cart> Carts { get; set; }
}
public class ProductsOfCart : IEntity
{
public virtual int Id { get; set; }
public virtual Product IdProduct { get; set; }
public virtual Cart IdCart { get; set; }
}
以下是Product,Cart和ProductsOfCart的XML文件
<class name="Product" table="Products">
<id name="Id" column="id_product">
<generator class="native" />
</id>
<property name="Name" column="name" />
<property name="Price" column="price" />
<property name="ProductState" column="productState" />
<property name="Maker" column="maker" />
<property name="Description" column="description" />
<property name="ProductWaranty" column="productWaranty" />
<many-to-one name="Category" column="id_category" foreign-key="id_category" />
<property name="ImageName" column="imageName" />
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
<id name="Id" column="id_cart">
<generator class="native" />
</id>
<property name="Price" column="price" />
<property name="PriceDph" column="priceDph" />
<property name="NumberOfItems" column="numberOfItems" />
<many-to-one name="IdUser" column="id_User" foreign-key="id_User" />
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<id name="Id" column="id_ProductsOfCart">
<generator class="native" />
</id>
<many-to-one name="IdProduct" column="id_product" foreign-key="id_product"/>
<many-to-one name="IdCart" column="id_cart" foreign-key="id_cart" />
</class>
答案 0 :(得分:0)
如果我们想要将配对对象创建为实体。我们需要这样的映射:
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_cart" />
<one-to-many class="ProductsOfCart" />
</bag>
</class>
<class name="ProductsOfCart" table="ProductsOfCart">
<many-to-one name="Product" column="id_product" />
<many-to-one name="Cart" column="id_cart" />
</class>
我们的课程看起来像:
public class Cart :IEntity
{
...
public virtual ILIst<ProductCategory> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<ProductCategory> Categories { get; set; }
}
public class ProductCategory : IEntity
{
...
public virtual Product Product{ get; set; }
public virtual Category Category { get; set; }
}
我喜欢这种结构。可以轻松地在其上创建复杂的查询...尝试阅读更多,例如many-to-many with extra columns nhibernate
但我们也可以隐藏配对表,并使用映射 many-to-many
,这将完全隐藏配对表并配对C#对象
<class name="Product" table="Products">
... // as is
<bag name="Categories" lazy="true"
table="ProductsOfCart" // many-to-many requires table defintion
inverse="true" batch-size="25"
cascade="none"> // cascade would not effect the pair, but Category
// usually not wanted
<key column="id_product" />
<many-to-many class="Category" column="id_product" />
</bag>
</class>
<class name="Cart" table="Carts">
... // as is
<bag name="Products" lazy="true" table="ProductsOfCart"
inverse="false" // one side MIGHT NOT be inverse
batch-size="25"
cascade="none"> // cascade would not effect the pair, but Product
// usually not wanted
<key column="id_cart" />
<many-to-many class="Product" column="id_product" />
</bag>
</class>
我们的课程看起来像:
public class Cart :IEntity
{
...
public virtual IList<Product> Products { get; set; }
}
public class Product : IEntity
{
...
public virtual IList<Category> Categories { get; set; }
}
在此处NHibernate map one-to-many relationship with intermediate table
了解多对多EXTEND
如果您提供No persiter ...例外,请确保:
.hbm.xml
(查看任何错字)Embedded resources
NH会话工厂配置
...
<mapping assembly="Project.DomainModel"/> <!-- Here -->
</session-factory>
此外,问题中的最新映射是正确的 - 而C#的东西不是。要有这个集合的映射:
(Product.hbm.xml)
...
<bag name="Carts" lazy="true"
inverse="true" batch-size="25" cascade="all-delete-orphan">
<key column="id_product" />
<one-to-many class="ProductsOfCart" />
</bag>
C#POCO必须是:
public class Product : IEntity
{
...
// instead of this
// public virtual IList<Cart> Carts { get; set; }
// we need this
public virtual IList<ProductsOfCart> Carts { get; set; }
}
这应该是足以使其运行的信息,我要说