I have a table called "Cart", which has composite key of 2 values:
public class Cart
{
[Column(Order=0), Key]
public int Id { get; set; }
[Column(Order=1), Key]
public int ProductId { get; set; }
public int Quantity { get; set; }
}
So, e.g. the same ids
of the "Cart" can have different ProductIds
(one cart with different products on it). Because of this Cart Id
column not being unique, I cant use DatabaseGeneratedOption.Identity
attribute on it to ask EF to create unique Ids.
When I insert new cart entity to database and iterate through products in the cart, is it possible to ask EF to generate new unique Id only once and return in to server?
int unqiueId = ??(dbcontext.Generate Unique ID For This Table)
foreach (var cartLine in cart.CartLines)
{
Cart cartLinePurchased = new Cart{
Id = uniqueId,
ProductId=cartLine.Product.Id,
Quantity=cartLine.Quantity
};
dbcontext.CartPurchased.Add(cartLinePurchased);
dbcontext.SaveChanges();
cartPurchased = cartLinePurchased;
}
Thanks in advance.