Attaching to .mdf

Jan 18, 2011 at 11:19 PM

I am following the example and when I get to the point of trying to display the list of genres the application is creating MvcMusicStore.Models.MusicStoreEntites in the SQLExpress Data Directory instead of using the mdf file in the APP_DATA folder.

Coordinator
Jan 18, 2011 at 11:39 PM

Is your database file being added in the correct folder, as shown in the steps at the top of http://www.asp.net/mvc/tutorials/mvc-music-store-part-4?

Jan 19, 2011 at 12:42 AM

Yes, it is in the APP_DATA folder. I just re-added to the folder but it still is attaching to the version of the database in the SQLExpress data directory instead of the one in the folder. Even if I use the version of the project from the zip file it gives me the same result.  Can it be the version of the entity framework I'm using. I'm using CTP5. 

Coordinator
Jan 27, 2011 at 1:16 AM

Can you copy in your connectionStrings section from your web.config? It should look exactly like this:

  <connectionStrings>
    <add name="MusicStoreEntities"
         connectionString="data source=.\SQLEXPRESS; 
        Integrated Security=SSPI;
         AttachDBFilename=|DataDirectory|\MvcMusicStore.mdf;
         User Instance=true"
          providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
Apr 7, 2011 at 8:02 AM
Edited Apr 7, 2011 at 8:03 AM

Im getting this error message when I try to open the mdf file within vs2010:

The database '*.MDF' cannot be opened because it is version 655. This server supports version 612 and earlier. A downgrade path is not supported.

Im using vs2010 and got sql express installed, what may have been the issue?

Coordinator
Apr 7, 2011 at 8:51 AM

If you have SQL Server 2005 installed, it may be the default instance. Can you check if this helps you? http://conceptdev.blogspot.com/2009/04/mdf-cannot-be-opened-because-it-is.html

May 21, 2011 at 11:46 PM

I'm using a Sql Server 2008 Database on a remote server(or local) and i couldn't connect to my database.

This is my connection string : <add name="LocalConnectionString" connectionString="Data Source=192.168.1.253;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=sa;Password=******" providerName="System.Data.SqlClient"/>

Can anyone give me the correct connection string to use.

Thanks

Coordinator
May 22, 2011 at 10:14 PM

HaythemTlili - I recommend that you test out your connection string using a UDL file. Here's a good blog post with instructions on how to do that: http://johnnycoder.com/blog/2010/06/12/database-connectivity-test-with-udl-file/

May 23, 2011 at 1:06 PM

Thank you Jon, the connection string worked for me and i have one more question.

I'm using Entity Framework 4.1 (Database First) and everything is going great but the Edit action does not work for me.

Here's the code :

 

[HttpPost]
public ActionResult Edit(Category category)
{
    if (ModelState.IsValid)
    {
        db.Entry(category).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(category);
}

And on the SaveChanges Method i got this : 

DbUpdateConcurrencyException was unhandled by user code.
Store update, insert, or delete statement affected an unexpected number of rows ({0}). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

And when i hit a break point i found the the CategoryID is 0.

Is there any solution ?

Thanks.
Jun 4, 2011 at 9:57 AM

I am experiencing exactly the same issue, been trying all work arounds but none successful so far, will post asap

Jun 5, 2011 at 12:12 AM
Edited Jun 5, 2011 at 12:39 AM

alrighty after going into a couple of debugs I did findthat the record id reverts to 0 (zero) where for example it needed to be 1 (or the same as the record id in the db), look at this solution,maybe it will help you, updated the forum with a fix that worked for me

http://mvcmusicstore.codeplex.com/workitem/6648

Nov 11, 2011 at 12:38 PM

Hi, I was having various problems trying to run the MvcMusicStore v3.0 against SQL Express and I worked out 2 possible solutions.

I got it running out of AppData as a .mdf file, or hosted on my .\SQLExpress instance using the SampleData.cs. I have full notes on my blog @

http://ntsblog.homedev.com.au/index.php/2011/11/11/how-to-run-mvcmusicstore-v3-0-with-sql-server-express/

Hope it helps, Note: I haven't tested up to the edit stage so I may update the article if I hit further problems. Cheers

Mar 27, 2012 at 10:08 AM
Edited Mar 27, 2012 at 10:13 AM
This DbUpdateConcurrencyException is due to this line in the Album.cs class : [Bind(Exclude = "AlbumId" )]
AlbumId is not mapped to the parameter of this function :
 
 [HttpPost]
        public ActionResult Edit(Album album)
        {}

either you remove the line  [Bind(Exclude = "AlbumId")] of the Album.cs class either you add in the method :

 [HttpPost]
        public ActionResult Edit(Album album)
        {
            if (ModelState.IsValid)
            {
                album.AlbumId = int.Parse(Request.Form["AlbumId"]);
//...
}
}

...
I don't understand though why they added this line on top of the Album.cs class.