I am trying to insert a record into a table using an Entity Framework. I made my database (mdf file), made a model from the database (edmx), and now I am trying to insert a record.
Users Brad = new Users
{
UserInitials="BCH",
UserName="Brad"
};
using (WeighProgramDatabaseEntities wDB = new WeighProgramDatabaseEntities())
{
wDB.Users.AddObject(Brad);
int res = wDB.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
List<Users> lwt = wDB.Users.ToList(); //call to look at the inserted data
}
When I follow this procedure it appears everything works fine. My wDB.Users.ToList(); returns one record which is the one I just inserted. However, if I run my code again, removing the insert stuff and just call the query (leaving just) List<Users> lwt = wDB.Users.ToList(); my query returns no records. What should I be doing differently to get my changes to persist? Why did my records "go away"?
WeighProgramDatabaseEntities inherits from ObjectContext.
This is my first attempt at using EF stuff so some of my verbiage might not be correct and I might not have included something important - let me know.
Edit:
I think my problem is that my Insert/Update/Delete functions are not mapped. But I'm not sure how to create a stored procedure for them to map to...
Edit2:
Ok, phew, I figured out the stored procedure mapping. and now that I have an INSERT mapped, and I call AddObject it will actually be inserted.
A little more complicated than I would have expected. For example if I right click on a table in my Database Explorer and select New Query it brings me to a great query designer window which let's me create Inserts, Selects, Updates... but there is no Save button! It's grey out. I need to write them from another route with does not let me generate Inserts/Deletes/Updates via a GUI. Which is ok I guess because they are not much text, but still a bit of a hassle.