views:

728

answers:

4

Hi guyz, I'm going crazy abou this, because I can't see what I'm doing wrong!!! Please help me!!!

I was developing a application using Entity Framework and storing data in a .mdf database, my code can read the data, apparently it can save too, but only apparently. I get no erros, while the program is running it act like the data was saved, I can for example save an object, dispose the context, create a new one, and then when I search my object he is there!!! But when I query the database to see the stored data there's nothing there!!! If I close the app and run it again, all data was gone! Here's a example code I wrote just to test:

        using (DatabaseEntities e = new DatabaseEntities())
        {
            for (int i = 0; i < 50; i++)
            {
                User u = new User();
                u.Nome = "User" + i.ToString();
                e.AddToUser(u);
            }
            int c = e.SaveChanges(true);

            List<User> us = e.User.Where<User>(x => x.ID < 50).ToList<User>();

            foreach (User u in us)
                Console.WriteLine("ID: " + u.ID + " Hello from " + u.Nome);

            Console.ReadKey();
        }

When I run this, I get the 50 outputs, if I look the content of the c variable in the debug, there's 50 changes, everythings seems to be fine, but when I start my query browser and look in the content of my MDF database, there's nothing there!!!

Probably it's something very simple but I can't see what it is, I need your help guyz!

+1  A: 

A few things leap to mind:

  • double/treble-check your connection string; are you really talking to the file that you think you are?
  • are you using transactions anywhere and not committing them?
  • do you have the mdf file set to "Copy Always" (or whatever it is).... i.e. are you constantly overwriting the mdf whenever you hit build/play?

Also - I don't think it'll matter in this case, but to verify data you should use a different entities/data-context:

using (DatabaseEntities e = new DatabaseEntities()) {
    for (int i = 0; i < 50; i++) {
        User u = new User();
        u.Nome = "User" + i.ToString();
        e.AddToUser(u);
    }
    int c = e.SaveChanges(true);
}
using (DatabaseEntities e = new DatabaseEntities()) {
    List<User> us = e.User.Where<User>(x => x.ID < 50).ToList<User>();
    foreach (User u in us)
        Console.WriteLine("ID: " + u.ID + " Hello from " + u.Nome);
}
Console.ReadKey();

Like I said - I doubt it'll matter here, but worth checking...

Marc Gravell
A: 

What's the type of ID ? A GUID? How do you set the ID? With NewID() as default? If that's the case, an O/R mapper can't read the ID back and will likely flag the entity as wrongly saved. Also check with SQL Profiler if you get any queries executed on the db.

Frans Bouma
A: 

Hi guyz, i'll check your tips when I get in home later, thankz a lot for answering!

Marc, yes, I'm talking with the correct .mdf, I know this because I added some data manually in the database, and then I've made a query to check if I could read the new data and I can! I'm not using transactions, my code is pretty simple, almost like what I've posted here.

Frans, the ID is a bigint in the database. When I create an object I don't set this ID, I just create it with a User user = new User(), then I set the properties and save it! It always worked for me!

aleemb, it step through, and like I said, if I close the context and open it again, the data is there. I can for example dispose the context, dispose my user class, then I search the user again in a new context and it find it, with all properties set correctly! I'm guessing it can be a connection string like you said, but the connection string is the default created by the visual studio, then I think it can be some parameter like Marc said! I really don't know. I've googled and didn't found anything like, so maybe it's a very stupid thing I'm doing wrong, a little detail, I don't know.

Thankz for the help! I'll make some tests in some hours.

+4  A: 

Guyz, I just found what was going wrong, and I guess there's nothing wrong actually. I forced an error, and then I could see the database that I was accessing was in the bin directory, the Visual Studio copied my database to the bin directory everytime I run the app, so that's why if I add some data manually I could see it, but the saves in the runtime aren't persisted. I always worked with database in servers, it's my first time with local databases, so I messed up it, but thankz a lot for the help!

Alaor
So, how did you work around this issue, if I may ask?
camainc
It was not a issue actually. The database that debug was using was a copy from the real database in the bin directory, then after debug, all changes in run time were loss because this temp database is renewed everytime it's build. In production it wouldn't happen.
Alaor
Yeah, I figured that out, finally. It wasn't exactly intuitive, but sometimes I'm pretty hard headed.
camainc