I've seen this question, this and this, however the following error is not gone:
Cannot insert explicit value for identity column in table 'Products' when IDENTITY_INSERT is set to OFF.
What I've tried is:
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
Moreover, I've tried to set to None:
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
T-SQL code looks like this:
CREATE TABLE Orders
(
Id INT IDENTITY(1,1),
DatePlaced datetime NOT NULL,
CONSTRAINT PK_Order_Id PRIMARY KEY (Id)
)
CREATE TABLE OrderItems
(
Id INT IDENTITY(1,1),
IdOrder INT NOT NULL
CONSTRAINT FK_OrderItems_IdOrder__Orders_Id FOREIGN KEY(IdOrder) REFERENCES Orders(Id),
IdProduct INT NOT NULL
CONSTRAINT FK_OrderItems_IdProduct__Products_Id FOREIGN KEY(IdProduct) REFERENCES Products(Id),
Quantity INT NOT NULL,
TotalPrice decimal (18,2),
CONSTRAINT PK_OrderItem_Id PRIMARY KEY (Id)
)
CREATE TABLE Products
(
Id INT IDENTITY(1,1),
Name varchar(100),
CONSTRAINT PK_Product_Id PRIMARY KEY (Id)
)
And model classes look like this:
public partial class Order
{
public Order()
{
OrderItems = new HashSet<OrderItem>();
}
public int Id { get; set; }
public DateTime DatePlaced { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
}
public partial class OrderItem
{
public int Id { get; set; }
public int Quantity { get; set; }
[Column(TypeName = "decimal(18,2)")]
public decimal? TotalPrice { get; set; }
public virtual Order Order { get; set; }
public virtual Product Product { get; set; }
}
public partial class Product
{
public Product() { }
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}
and Customer model:
public class Customer : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
What I want is to save Order and OrderItems. The code of saving looks like this:
public async Task Add(Order order)
{
order.Customer = _context.Customers.Find(order.Customer.Id);
await _context.AddAsync(order);
await _context.SaveChangesAsync();
}
and OrderService class:
public async Task<OrderDto> Add(OrderDto orderDto)
{
var order = _mapper.Map<Order>(orderDto);
await _orderRepository.Add(order);
orderDto.Id = order.Id;
return orderDto;
}
Please, tell me what I am doing wrong? Please, do not close my question as it is not duplicate.
Productstable, but the code you've shown is for theOrdersandOrderItemstables. Can you show the code forProducts? - devNullAddAsyncadds the entity to the context in the Added state so it's trying to insert it. It already has an id because you just did aFindof it (presumably). I think you can just remove the second line of code in theAdd()method. - Crowcoder