2

I am learning MVC4 from http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller (edit: Fixed URL)

Everything is working perfectly but my database isn't getting updated but when I run the project the records are coming from somewhere and I couldn't find that database in my MSSQL.

My connection string in web.config is

<connectionStrings>
    <add name="PurchaseInfosDbContext" 
         connectionString="server=lakpa-pc;Integrated Security=SSPI; User ID=sa; Password=xxxxx; database=MessInfo" providerName="System.Data.SqlClient" />
</connectionStrings>

but in the Visual studio when I debug I get the connection string as

"Data Source=.\\SQLEXPRESS;Initial Catalog=Mvc4Projects.Models.ItemsDetailsDbContext;Integrated Security=True;MultipleActiveResultSets=True"

Isn't it supposed to read the connection string from web.config. I haven't modified any connection string on any pages.

a. Is it necessary to include SDF file as shown in that tutorial? Can't I directly update it to mdf file?

1
  • My tutorial uses LocalDB. <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20120816100809;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20120816100809.mdf" providerName="System.Data.SqlClient" /> </connectionStrings> Commented Aug 16, 2012 at 17:09

4 Answers 4

5

My problem was that the name of the class that was inheriting DbContext was different so MVC was taking default connection to SQLExpress even though I didn't specify it.

Well I changed the Class Name similar to Connection String name and now its pointing to correction location.

May be it will be helpful to someone. Problem faced and resolved

a. The Class that inherits DbContext must be used as a name for connection string.

b. The SQL Query that Entity framework add has plural table name so use the attribute [Table("PurchaseInfo")] to make it singular.

c. When using POCO method you will encounter a key problem if you don't follow its naming convention like in my example its Key must be "PurchaseInfoID" but I used ItemId . So use the attribute [Key] to solve this problem.

public  class PurchaseInfoDbContext : DbContext
    {
        public DbSet<PurchaseInfo> ItemsDetails { get; set; }
    }

    [DisplayName("Items Details")]
    [Table("PurchaseInfo")]
    public class PurchaseInfo
    {
        [HiddenInput]
        [Key]
        public int ItemId { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I have the same problem, if you already have App_Data Folder delete it and then add a new one from Project-> Add ASP.Net folder -> App_Data

1 Comment

Sorry but how would that fix the problem specified here? Can you elaborate your answer?
0

When you start a new MVC 4 project with the "Internet Application" it adds a default connection string that points to a sql express database. Are you sure that you don't have that connection string in your web.config still?

1 Comment

There is only one connection string in web.config. I search for all SQLExpress in whole project but I couldn't find it. But when I debug I see that SQLExpress connection string. When I open my MSSQL as SQLExpress, I see that it has created different database. That wasn't the one I was expecting.
0

I found this article - http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-1-user-profile.aspx which resolves the issue. However this applies if user is running the App through IIS

Comments

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.