Here is my code after modifications, things seems working fine but I am receiving an error on _db.SubmitChanges();
Error is
Cannot add an entity with a key that is already in use.
Code:
foreach (tblCourseNeededHours record in thisTable)
{
tblCourseNeededHours newCNHR = new tblCourseNeededHours();
newCNHR.Semester = a_semesterToOrganize;
newCNHR.AssignToInstituteAdministrator = false;
newCNHR.Freezed = false;
_db.tblCourseNeededHours.InsertOnSubmit(newCNHR);
}
// submit all the changes
_db.SubmitChanges();
I am using MVC 2.0 and SQL Server. I have a table called tblCourses.
I want to select rows based on some selection criteria, then I want to append these rows to tblCourse.
Do I need to create a temporary table tmpCourse and to fill in these selected rows and then append them those back to tblCourse? Or can I do it without temporary table?
Any suggestion, post link ?
I believe you can just do something like:
Of course, the list of columns must match, e.g. you have to have the same number of columns, and the same datatypes. Also, you cannot insert values into e.g.
IDENTITYor computed columns.Update: to do this in Linq-to-SQL, you’d have to have an entity that can represent your data in some way. Then:
List<Entity>(or whatever your entity is really called)Something along the lines of this code snippet (here I have a table
countrieswhich has anISOCodeand aCountryNamefor some countries; I’m selecting a few, and creating new ones based on those retrieved, adding those new ones to the Linq-to-SQLDataContextand saving in the end):