I'm new in entity framework. Have a code:
public Owner AddOwnerForProduct(int productId, string ownerName)
{
using (var context = new Entities())
{
var existingOwner = context.Owners
.SingleOrDefault(owner => owner.ProductId == productId);
if (existingOwner != null)
return existingOwner;
var newOwner = new Owner
{
ProductId = productId,
Name = ownerName
};
context.Owners.Add(newOwner);
context.SaveChanges();
return newOwner;
}
}
The Owner can be only one. If the Owner for ProductId already exists should be returned an existing Owner entity. What do I need to add in the code if I know that it is possible more than one method call at a time (multithreading) for the same productId? I think not lock statement?