【问题标题】:SQL Connection String in MVC ApplicationMVC 应用程序中的 SQL 连接字符串
【发布时间】:2015-01-06 07:49:28
【问题描述】:

我正在开发一个 MVC 应用程序,到目前为止,我一直在使用 localDB,因为它包含在教程中。我现在想将应用程序切换到我的 SQL Server,但我不知道该怎么做。

我知道我必须更改连接字符串。但不是以什么方式,因为 SQL Server 有一个我的 localDB 没有的用户名和密码。

关于此的另一个问题是,我是否必须在开始时自己在 SQL Server 上创建表,或者它们是否会像我的 localDB 一样由实体框架生成?

localDB 的当前连接字符串:

<add name="AcquisitionDBContext"
   connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Acquisitions.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

我对此很陌生,因此非常感谢任何帮助

【问题讨论】:

  • 这是格式:Server=myServerAddress;Database=myDataBase;User Id=myUsername;密码=myPassword;

标签: c# sql-server asp.net-mvc


【解决方案1】:

如果你打算使用 MS SQL,那么它看起来像这样:

<add name="AcquisitionDBContext" connectionString="Data Source=hostname\sql_instance_name;Initial Catalog=databaseName;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

其中“hostname\sql_instance_name”是服务器名称(或 ip)/sql 实例名称,例如。本地主机\sqlexpress

【讨论】:

  • 好的,所以当我打开 sql 管理工作室时,它会显示: BLOEK(SQL Server 12.0.2000 - Bloek\Ride) 所以我猜对我来说它将作为数据源“Bloek\Ride”。对吗?
【解决方案2】:

ConnectionStrings.com 是连接字符串语法的最佳参考。它向您显示当您拥有用户名+密码时要使用的选项。在这种情况下:

Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;

Entity Framework 不会为你创建表,除非你通过调用这样的方法告诉它:

Database.SetInitializer( new DropCreateDatabaseIfModelChanges<YourDbContextHere>() );

(其中DatabaseSystem.Data.Entity.Database 类型)。

【讨论】:

  • 注意:我应该在我的应用程序中的哪里设置初始化程序?在模型中?
  • 好的,非常感谢。刚试了一下,报错:{"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct."} 是不是因为我还没有使用初始化器?
  • @VIP InnerException的细节是什么?
【解决方案3】:

试试看:

 <connectionStrings>
<add name="<<Name of Connection String>>" connectionString="Data Source=<<Put here path to your SQL SErver>>;Initial Catalog=<<Database name>> ;Integrated Security=SSPI;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>

【讨论】:

    【解决方案4】:

    connection string 应该是这样的(根据您的需要进行调整)

    <add name="DefaultConnection" connectionString="Data Source=server_name,port; Initial Catalog=your_catalogue_name; Integrated Security=False; User ID=userName;Password=Passwd;     MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
    

    在第一次运行之前,我建议使用migrations。 转到PackageManagerConsole(如果菜单中没有,请先通过Extension and Updates 安装NuGet

    然后在控制台输入Enable-Migrations(如果你还没有)。 接下来输入Add-Migration your_migration_name,然后输入Update-Database,让migrations 为您创建表格。

    如果您稍后更改模型文件中的某些内容,您可以再次键入 Add-Migration your_migration_nameUpdate-Database 以使用这些更改更新数据库。

    【讨论】:

    • 是否有可能在代码中执行此第二部分?我无法通过 Visual Studio 计算机直接访问 sql 服务器,因为它位于不同的受保护网络中。
    • @VIP 我从没听说过这种可能性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2013-09-27
    相关资源
    最近更新 更多