3

I'm trying to use Entity Framework 4 for a small database application I'm writing to keep record of downloaded files. When running the application I set a break point after the tableName.Add() method, before the .SaveChanges() method and I can see the data saved into the entity; then I have another break point after calling the .SaveChanges() method, and look into the database to find there is no record saved to it. I have found a lot of similar questions, but I have not found the solution to my particular problem. Here is the code:

    public void StartNewDownload(string FileID)
    {
        DateTime startTime = DateTime.Now;

        FilesDBEntities db = new FilesDBEntities();
        int startedID = (from dr in db.tblDownloadResults
                         where dr.Value.Equals("Started")
                         select dr.ResultID).First();
        tblDownloads myDownload = new tblDownloads { FileID = FileID, StartDateTime = startTime, ResultID = startedID };
        db.tblDownloads.Add(myDownload);
        db.SaveChanges();
    }

Any help is appreciated.

Thanks!

4
  • 1
    How do you "look into the database"? Have you tried reading the entity in your app? Did it work? I have seen many cases where people where using CodeFirst and were looking at a wrong database since they changed something and CodeFirst created a new database where data was saved. If you can read what your saved in your app that it means that the data is somewhere saved. Commented Oct 23, 2012 at 3:32
  • Pawel, Thanks for the reply. I can see this being a potential cause. Will look into this and report back. Thanks. Commented Oct 23, 2012 at 21:42
  • You can always get the connection string using ((IObjectContext)ctx).ObjectContext).Connection.ConnectionString which will tell you what database you are hitting. Commented Oct 23, 2012 at 21:44
  • @Pawel What the HELLL. I was also looking at the wrong db in visual studio. It was saving it in /bin directory whereas the connection string was of the db in /AppData folder & I was checking that. Commented Nov 26, 2015 at 17:08

2 Answers 2

5

Pawel, you guided me in the right direction. The entity had the data, but the database I was looking into did not. But after reading your comment I ran the program from Visual Studio and used Process Monitor to monitor any operations to *.sdf files. This helped finding out that upon building the solution, it would create another database file to the bin\Debug folder. I forgot the database Build Action property was set as "Content".

Thanks!!

Sign up to request clarification or add additional context in comments.

Comments

0

You can use SQL Server Profiler to see if the entity framework has really called the database.

(The tool is not included in SQL Server Express)

1 Comment

Edi, thanks for your reply, and sorry for not including additional information. I'm using SQL Compact instead of SQL Express. Would SQL Profiler still be useful in this case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.