I write a service can reach values of weekly up to date exchange rates.
But I get an error when sending the values to the database.I created a database model in Entity Framework. The associated database tables names are CURRENCY and WEEKLY_VALUE.
Related fields are KOD field in CURRENCY and KOD field in WEEKLY_VALUE table. KOD field in WEEKLY_VALUE is Foreing_key.
I get an error in this code:
public void insertWeeklyCurrency(List<CURRENCY> currencyList)
{
myEntity = new CurrencyEntities();
DateTime date = new DateTime();
date = System.DateTime.Now;
String day= date.DayOfWeek.ToString();
if (!day.Equals("Sunday") && !day.Equals("Saturday"))
{
WEEKLY_VALUE weeklyCurrency;
for (int i = 0; i < currencyList.Count; i++)
{
weeklyCurrency = new WEEKLY_VALUE();
weeklyCurrency.KOD = currencyList[i].KOD;
weeklyCurrency.TARIH = currencyList[i].TARIH;
weeklyCurrency.DEGER = currencyList[i].F_SATIS;
weeklyCurrency.CURRENCYReference.Value=currencyList[i];
myEntity.AddToWEEKLY_VALUE(weeklyCurrency);
myEntity.SaveChanges();
}
}
}
The error message:
{"Violation of PRIMARY KEY constraint 'PK_CURRENCY'. Cannot insert duplicate key in object 'dbo.CURRENCY'.\r\nThe statement has been terminated."}
How can handle this error?