using the Repository pattern, I am first querying the same database in the code block below, and then processing according to the query result. However, it adds the same data to the database more than once in the api request that comes in different milliseconds.
I used transantion scope because I was adding to two databases at the same time.
I have shared the sample code block and the database screenshot below(the first databases I query and add are the same).
public void Add(AddCookieRequest request)
{
var beforeCookie = ICookieRepository.Get(m => m.IP == request.IP && m.FK_OrganizationGuid == new Guid(request.FK_OrganizationGuid));
if (beforeCookie == null)
{
using (var scope = new TransactionScope())
{
var cookie = new Entities.Cookie
{
City = request.City,
ContinentCode = request.ContinentCode,
ContinentName = request.ContinentName,
CountryCode = request.CountryCode,
CountryName = request.CountryName,
FK_OrganizationGuid = new Guid(request.FK_OrganizationGuid),
FK_CookieContentId = request.FK_CookieContentId,
IP = request.IP,
StateProv = request.StateProv,
Status = request.Status,
};
ICookieRepository.Add(cookie);
request.SelectedCookies.ToList().ForEach(c =>
{
cookiePermissionRepository.Add(new CookiePermission
{
FK_CookieId = cookie.Id,
FK_OrganizationCookiePermissionId = c.Id,
IsSelected = c.IsSelected,
FK_OrganizationGuid = new Guid(request.FK_OrganizationGuid),
});
});
scope.Complete();
}
}
}