0

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.

Shukhrat Raimov
  • 2,137
  • 4
  • 21
  • 27
  • What purpose does having multiple rows with the same `Id` serve? – Lasse V. Karlsen Nov 15 '14 at 21:17
  • The same Id means the same Cart. One cart(one unique id) with different Products on it. For instance cart with Id = 1, has 1,2,3,4,5 productIds on it; cart with Id = 2, has 3,5,7,8 productIds on it; etc. That is why I created composite key. – Shukhrat Raimov Nov 15 '14 at 21:20
  • 2
    How about using a Guid instead? Or having a separate cart table which would be responsible for giving you those unique numbers? – Lasse V. Karlsen Nov 15 '14 at 21:21
  • 1
    Supporting Lasse V. Karlsen: My Cart would have an id and a foreign key to the customer loading it. My CartLine would have foreign keys to Cart and Product as well as the Quantity. - No more trouble with unique Cart ids. – Abecee Nov 15 '14 at 21:25
  • I was thinking a lot about Guid and I did not like this solution. I know that a chance is really small to create the same Id for two rows, but I want this chance to be 0 not small:) Separate cart table is really good idea, but I am just curious if EF can do this for you on its own by looking on the Id column? – Shukhrat Raimov Nov 15 '14 at 21:25
  • @Abecee yes, probably I go with it. My deepest thanks to you guys! – Shukhrat Raimov Nov 15 '14 at 21:27
  • 1
    @ShukhratRaimov: You can still check to see if a given `Guid` exists before committing it. However the [probability of accidentally generating a duplicate `Guid` is fairly unlikely](http://stackoverflow.com/questions/1705008/simple-proof-that-guid-is-not-unique)... – grovesNL Nov 15 '14 at 21:37

1 Answers1

2

First: there is no way for EF to create a compound key unique value. Please, see this SO answer.

Second: your database is denormalized. You need to normalize it. There is a cart which can have several products, then:

  • you need a cart table, which has the cart id (can be a single PK, generated by the DB using identity, and this is supported by EF). This cart must only have data related directly to the cart (for example id of the cart, id of the customer, creation date of the cart...)
  • you need a productInCart table which holds all the products of a cart. This table must have an FK that relates each row of a productInCart with the corresponding cart using the cart id. This can have data related to the products in the cart, like product, quantity, unit price, total price...

You can read this to learn about database normalization

Things will become much easier if you do them this way. If not you'll get a lot of trouble, apart of the generation of the PK for each row.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117