0

I can't update the database but when im debuging it, production is updated and created records in context. But when I close the debugging, the database has no any Data Definition.

I enjoyed coding until I got stocked up with this 'not updating' SubmitChanges() method to the database(but updates the context). It really kills me, i'm stocked about few hours finding solution in the web.

By the way, I set 'id' as Production PK with Identity incremented in pk property. Also in .dbml file, I set also set auto increment. Below is my code:

 qmsDBDataContext context = new qmsDBDataContext();
 public void AddProduction(int quails, int eggs, int feeds, int id_box) {
        Production production = new Production();
        production.quails = quails;
        production.eggs = eggs;
        production.id_box = id_box;
        production.feeds = feeds;
        context.Productions.InsertOnSubmit(production);
        context.SubmitChanges();
}
Jed
  • 1,664
  • 8
  • 33
  • 65
  • 1
    What is the value of production.id after you call SubmitChanges. If it is zero then either all the parameters are zero or your definition/dbml is wrong. If it is non-zero, then either you are inside a transaction which is not getting completed (eg a TransactionScope which isn't getting completed) or the record is being saved correctly. – sgmoore Oct 06 '12 at 16:55
  • @sgmoore starts from 1 cause i set seed =1, then 2 for another added record and so on and so forth... – Jed Oct 06 '12 at 16:59
  • @sgmoore What would be that transaction? im not yet familiar with TransactionScope thing... Is it the production.id that i did not put a value? – Jed Oct 06 '12 at 17:02
  • 1
    For transactions see http://stackoverflow.com/questions/542525/transactionscope-vs-transaction-in-linq-to-sql - but it is unlikely you are using it without knowing (unless you copied someone elses code). When you close and restart the program does your ids start back at 1. Also can you check the value of context.Connection.ConnectionString to make sure your program is accessing the correct database and make sure nothing is deleting the records or even deleting/recreating the database. If you have access to SQL profiler, it might help see what SQL commands are hitting your sql server. – sgmoore Oct 06 '12 at 17:19
  • @sgmoore I just recently learned how to use LINQ to SQL in reading HeadFirst C#. What I just read is how to get data from the database. Then i search sites to Insert,Update and Delete Using Linq2SQL. And all those sites have the same method to commit changes to be written in the database which is dataContext.SubmitChanges(); It seems i made the necessary things to do to map the db in ORM because im accessing it already and displaying the Data I manually wrote in the database. – Jed Oct 06 '12 at 18:04

1 Answers1

0

I found a fix exactly answers my problem it was explained here . The mdf file I was working on existed in my project/debug folder, and the mdf file that server explorer was looking at existed in the project folder. Thanks to Matt Warren - MSFT to answering this question. Cheers

@sgmoore Thanks for giving me ideas about Transaction Scope Thing.

Jed
  • 1,664
  • 8
  • 33
  • 65