2

I just started with Entity framework and I was able to do select operations using linq,but I had an issue with Inserting. I tried this sample to see whats wrong :

 testEntities te = new testEntities(); te.Customer.Add(new Table {Id=1,Credit=200m,Name="test" }); te.SaveChanges(); 

Well it appears that the changes the row was only added to the testEntites but not to the database and when I restart the application I find no rows added. Thanks,

14
  • Do you get any error? Are you using any transaction scope? Commented Feb 20, 2015 at 18:06
  • Do you get any errors? Try creating var t = new Table {Id=1,Credit=200m,Name="test" }; t.markAsAdded(); te.Customer.ApplyChanges(t); te.SaveChanges; Commented Feb 20, 2015 at 18:07
  • What type is te.Customer? Commented Feb 20, 2015 at 18:09
  • Also, what kind of database? If local sql server (express) database, check if you've set it to "copy to output: always". Commented Feb 20, 2015 at 18:10
  • 2
    Are you using a local database file? Are you certain it's not getting overwritten when you build the solution? Commented Feb 20, 2015 at 18:36

2 Answers 2

2

One way I fixed it was by making sure I was putting all the required fields on database. I was getting the same error before. I checked my database and I was not putting the not-nullable field when I was making the query. That solve my issue.

Sign up to request clarification or add additional context in comments.

Comments

0

I had this problem while deleting records. Not sure what causes this, but I fixed it by forcibly changing the state of the entity. Some pseudo code:

Customer CustomerRecord = db.Customer.Find(Id); te.Entry(CustomerRecord).State = System.Data.Entity.EntityState.Added; te.SaveChanges(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.