I want to map a M:N asociation betwen Products and Cart with asociation class ProductsOfCart betwen them. However data is not persister but I dont know why can you help me?
Here is a code from 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);
Cart, Product and ProductsOfCart
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; }
}
And here are XML files of Product, Cart and ProductsOfCart
<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>