Getting the error "cannot add an entity that already exists." while inserting a table's value in to DB without procedure
c#, linq
Solution
Finally After Much hassle I got my answer with a new concept..
For all those facing similar problems,here is a clear solution:
You need to create the object of the 'Table' class(which object you want to update in DB), inside the loop(*for each,for or any other..)so that same record is not updated and each object uses a different memory location(as the local instance gets destroyed inside the loop only..)*
Problem
I am inserting a list of records to the DB table with Linq to sql like this: ``` //my DataContext Class using (VTMMedicalDBDataContext objVTMMedicalDBDataContext = new VTMMedicalDBDataContext()) { ReadOnlyCollection<TimeZoneInfo> objTimeZones = null; objTimeZones = TimeZoneInfo.GetSystemTimeZones(); if (objTimeZones.Count > 0) { //List<TimeZoneMaster> listTimeZones = new List<TimeZoneMaster>(); TimeZoneMaster objTimeZoneMaster = new TimeZoneMaster(); foreach (var timezone in objTimeZones.ToList()) { objTimeZoneMaster.TimeZoneName = timezone.DisplayName; var localName = timezone.DisplayName; objTimeZoneMaster.TimeZoneOffsetInMinutes = Convert.ToInt32(timezone.BaseUtcOffset.TotalMinutes); objVTMMedicalDBDataContext.TimeZoneMasters.InsertOnSubmit(objTimeZoneMaster); objVTMMedicalDBDataContext.SubmitChanges(); } } } ``` I have a primary key but I have already made it as AutoGeneratd as true in DBML Nullable as false.Still I am not able to get rid of it...Please suggest some way around.