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();
}
UPDATE: I did the following:
Commands:
Removed the following duplicate tag in
_Layout.cshtml
<script src="~/js/site.js" asp-append-version="true"></script>
In summary
It is a mistery to me why you didn't catch this before via debugging
EF ModelBuilder
Your configuration should look like:
Having a value generated automatically does not define it as the primary key