asp.net-mvc ASP.net MVC 4,连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11964319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.net MVC 4, Connection string
提问by Lakpa Sherpa
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)
我正在从http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller学习 MVC4 (编辑:固定网址)
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.
一切正常,但我的数据库没有更新,但是当我运行项目时,记录来自某个地方,我在 MSSQL 中找不到该数据库。
My connection string in web.config is
我在 web.config 中的连接字符串是
<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
但是在调试时在 Visual Studio 中我得到的连接字符串为
"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.
是不是应该从 web.config 读取连接字符串。我没有修改任何页面上的任何连接字符串。
a. Is it necessary to include SDF file as shown in that tutorial? Can't I directly update it to mdf file?
一种。是否有必要包含该教程中所示的 SDF 文件?我不能直接将其更新为 mdf 文件吗?
回答by Lakpa Sherpa
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.
我的问题是继承 DbContext 的类的名称不同,因此即使我没有指定它,MVC 也会采用与 SQLExpress 的默认连接。
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.
一种。继承 DbContext 的 Class 必须用作连接字符串的名称。
b. The SQL Query that Entity framework add has plural table name so use the attribute [Table("PurchaseInfo")] to make it singular.
湾 Entity 框架添加的 SQL Query 具有复数表名,因此使用属性 [Table("PurchaseInfo")] 使其成为单数。
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.
C。使用 POCO 方法时,如果您不遵循其命名约定,就像我的示例中那样,它的 Key 必须是“PurchaseInfoID”,但我使用的是 ItemId ,那么您将遇到一个关键问题。所以使用属性[Key]来解决这个问题。
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; }
}
回答by Rami Ebeid
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
我有同样的问题,如果您已经有 App_Data 文件夹,请删除它,然后从 Project-> Add ASP.Net 文件夹-> App_Data 添加一个新文件夹
回答by Pieter Germishuys
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?
当您使用“Internet 应用程序”启动一个新的 MVC 4 项目时,它会添加一个指向 sql express 数据库的默认连接字符串。你确定你的 web.config 中没有那个连接字符串吗?
回答by Beena
I found this article - http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-1-user-profile.aspxwhich resolves the issue. However this applies if user is running the App through IIS
我发现这篇文章 - http://blogs.msdn.com/b/sqlexpress/archive/2011/12/09/using-localdb-with-full-iis-part-1-user-profile.aspx解决了这个问题. 但是,这适用于用户通过 IIS 运行应用程序的情况

