I have an .mdf file which I’m trying to add a record to, using linq in C#.
My code is:
dbDataContext context = new dbDataContext();
book b = new book();
b.title = "Test Book";
b.isbn = "123789";
context.books.InsertOnSubmit(b);
context.SubmitChanges();
When this code runs, the record is not inserted, and I get no error messages. If I use the database explorer to add a record with the selfsame data, it works.
What’s going on?
Generally when this happens it is indicative of the changes going to the incorrect database. Check your connection settings in your dbml. To test out the theory, try adding a record or two programmatically and if you have an identity field in that database, after context.SubmitChanges() check what the value of b.Id is (where Id is equal to your IDENTITY column). If it has a value, then you most definitely have a connection issue & you should check for another database. Pay particular attention to your bin/debug directory.