Vynhoû Tawa
Vynhoû Tawa

Reputation: 68

Local database not update after SaveChanges with entity framework

I would like to create a winform application using a local database (.mdf) and entity framework.

I'have created the database and the model. When I execute this code:

using (var context = new testEntity())
{
    CLIENTS c = new CLIENTS();
    c.Name = "name";
    c.Age = 10;
    context.CLIENTS.Add(c);
    context.SaveChanges();
}

a client is created in the database and I can get it with this code:

using (var context = new testEntity())
{
    List<CLIENTS> clients = context.CLIENTS.ToList();
    //break point here
}

But the added client is not on the .mdf file after closing the program. If I add some lines in the .mdf file, I can display them but not the opposite.

If someone can help me, thank you

Upvotes: 0

Views: 1647

Answers (3)

J-R Choiniere
J-R Choiniere

Reputation: 694

Take a look of your connection string in the config file (App.config) When you generate a .edmx from a database, it makes a copy of your database in the debug folder(if you run on debug) if you clicked on yes when it was asked. So as it was say, every time you rebuild, the content of the copy of your database is cleared.

You have to change the connection string to point to your initial dabatase.

Upvotes: 0

Ankush Madankar
Ankush Madankar

Reputation: 3834

.mdf file lost changes every time when you rebuild your code: Solution: Keep your .mdf file other location rather than build folder location, e.g. C or D drive, make respective changes in connection string. You can also refer Pranav's answer.

Upvotes: 1

Pranav Patel
Pranav Patel

Reputation: 1559

check the Copy to Output Directory of DB.
change the Copy to Output Directory property of the database file to Copy if newer..

Upvotes: 6

Related Questions