tags:

views:

45

answers:

1

I get a System.Data.Linq.DuplicateKeyException when adding an entity to an empty table.

grade g = new grade();
g.subject = dc.subjects.Single(x => x.subjectID == 5);
g.student = aStudent;
dc.grades.InsertOnSubmit(g);

I dropped the entire table itself and recreated it with no luck. Grades is a join table with subjectID and studentID as foreign keys.

Any ideas?

A: 

There might be problem with the primary key that is stored to be the next one. Try the following:

DBCC CHECKIDENT("Grades", RESEED, 1)

You should also check the foreign key like this:

DBCC CHECKIDENT ("Subjects", NORESEED)
Teddy