简体   繁体   中英

LINQ Insert - No Error but no insert either

I'm using the following code to insert a new entry in my existing db. But the new record doesn't get inserted. What are the options to fetch a possible exception? What could be the reason that this doesn't work?

Thanks,
rAyt

using (ContactManagerSampleDataDataContext db = new ContactManagerSampleDataDataContext())
                    {
                        CustomerCompany company = new CustomerCompany();
                        company.CompanyName = "Test";
                        company.IsActive = true;
                        company.ModifiedDate = DateTime.UtcNow;
                        company.SapNumber = 1;
                        company.CompanyId = 1;

                        db.CustomerCompanies.InsertOnSubmit(company);
                        db.SubmitChanges();
                    }

If SubmitChanges is asynchronous, then the db object could be disposed before it has chance to finish, however, I don't see any evidence that it is asynchronous. Perhaps wrapping the SubmitChanges call in a try/catch block will indicate any exceptions that have occurred. You could also look at the DataContext.ChangeConflicts to see if any conflicts occurred.

You could wrap the db.SubmitChanges() with a try catch and see if anything is thrown. It could also be a concurrency issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM