My Entities are:
public class Customer
{
...
public virtual ICollection<ShoppingCartItem> ShoppingCartItems { get; set; }
...
}
public class ShoppingCartItem
{
public string CustomerId { get; set; }
public int ProductId { get; set; }
public virtual Customer { get; set; }
public virtual Product{ get; set; }
...
}
The add method is:
public async Task AddAsync(TEntity entity)
{
await Task.Factory.StartNew(() => this.entities.Add(entity));
}
The entity that I am adding is:
ShoppingCartItem()
{
CustomerId = "xxxxx",
ProductId = 1,
Customer = null,
Product = null
}
When I call SaveChanges() the EF is trying to insert two identical records for ShoppingCartItem. The ShoppingCartItem is created and added to the context only once. Any ideas what possibly could be wrong?
EDIT:
This is how I call the AddSync method:
public async Task AddNewCartItem(ShoppingCartItem shopingCartItem)
{
await this.ShoppingCartItemRepository.AddAsync(shopingCartItem);
await this.SmartStoreWorkData.CompleteAsync();
}

ShoppingCartItemhas primary key. @DavidG either ways the EF is trying to add two records.