0

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Brad
  • 11,934
  • 4
  • 45
  • 73
  • You don't need stored procedures. Your code should work. Are you using your own transaction (for example `TransactionScope`)? – Ladislav Mrnka Feb 26 '12 at 19:42
  • @LadislavMrnka I am not using my own transaction. The code above is literally the only thing I wrote to interact with the database. I would like to think it should just work but alas...it did not. – Brad Feb 26 '12 at 20:21
  • I get it! It has all come together. I did not see the `Linq to SQL classes` option in the Add New Item... screen. I didn't have a `dbml` file and it was all throwing me for a loop. Everything works now with minimal effort. – Brad Feb 26 '12 at 20:50
  • How is Linq-to-Sql and dbml related to the problem? You mentioned Entity framework but neither L2S or DBML has nothing to do with EF. – Ladislav Mrnka Feb 26 '12 at 21:36
  • Yes...so this goes right in line with the last sentence of my original post. First time doing this, I'm trying to get the nomenclature correct but there is a lot to learn this weekend! – Brad Feb 26 '12 at 21:48

2 Answers2

6

I think that your ConnectionString points to the mdf in the Debug/Release folder of your solution, and that file is probably overwritten each time you build your application that's why you think your changes are not persisted. Try setting the ConnectionString to point to the mdf in the project folder (the one that is actually reference by the project).

dmusial
  • 1,504
  • 15
  • 14
  • Boy, I have come a long way in the last few hours and it turns out this was one of my biggest problems. – Brad Feb 27 '12 at 03:23
1

You can also set the "Copy to output directory" file property to "Copy if newer" rather than "Copy always". If you update the schema of the database in your project then it becomes newer than the database file in the debug directory and it will be overwritten on compile.

mjhillman
  • 59
  • 4