# DbContext.SaveChanges not saving to database



## ipv89

Hi All,

I am writing a asp.net MVC application in C#. I have a method that is used to create a new user. I had tested the code twice then left it and made come changes to the view in another part of the application.

Now when I try to add a new user nothing happens, I can see that the data is in the newuser object (db.Users.Add(newuser)







but when the line db.SaveChanges(); runs nothing happens no error is produced and the function carries on.

any ideas?

Code:



Code:


 private App_DatabaseEntities db = new App_DatabaseEntities();

public ActionResult Create(Student newuser)
        {
            try
            {
                // Add new instance of student
                db.Students.Add(newuser);
                db.Students.Attach(newuser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {

                return View();
            }
        }


----------



## Sisaroth

I think you have to remove attach.
Quote:


> Note that calling Add or Attach for an entity that is already tracked can also be used to change the entity state. For example, calling Attach for an entity that is currently in the Added state will change its state to Unchanged.


So EF thinks it's unchanged and will not try to insert or update, because EF doesn't do anything you don't get an exception is my guess of what's happening.

source: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

Use just add or do:

Code:



Code:


db.Students.Entry(newuser).State = EntityState.Added;
db.Students.SaveChanges();


----------



## ipv89

Quote:


> Originally Posted by *Sisaroth*
> 
> I think you have to remove attach.
> So EF thinks it's unchanged and will not try to insert or update, because EF doesn't do anything you don't get an exception is my guess of what's happening.
> 
> source: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx
> 
> Use just add or do:
> 
> Code:
> 
> 
> 
> Code:
> 
> 
> db.Students.Entry(newuser).State = EntityState.Added;
> db.Students.SaveChanges();


Thank you that worked


----------



## ipv89

Another question if you don't mind. I have created a database for all of the data I use in the application. The MVC template generated a login page that works but it created its own database called DefaultConnection. Can this be merged with my database? should I scrap the model,view,controller and database for user login and start from scratch?


----------



## randomizer

If your database has the same schema you could just change the connection string used by EF in your Web.config and remove the extra one that the scaffolding created, along with any EF class and template files. You can also rename the database and get rid of yours. You'll need to modify the entity context class, the Web.config and possibly some other generated files. I don't recall the exact process because it depends on your EF version as well as whether you've chosen a Database First or Code First approach.


----------

