Const
Const

Reputation: 131

SaveChanges() method not saving changes to the database

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,

Upvotes: 2

Views: 1528

Answers (2)

Sandip Subedi
Sandip Subedi

Reputation: 1077

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.

Upvotes: 2

Lee Harrison
Lee Harrison

Reputation: 2453

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();

Upvotes: 0

Related Questions