简体   繁体   English

在实体框架中连接到未知数据库

[英]Connected to Unknown Database in Entity Framework

Here is my connection string in web.config: 这是我在web.config中的连接字符串:

在此处输入图片说明

Note database name isn't there in connection string, but I can perform all CRUD operations using EF in MVC 5. 注意,连接字符串中没有数据库名称,但是我可以在MVC 5中使用EF执行所有CRUD操作。

Can someone tell how I can get to which database I am connected in MSSQL Server. 有人可以告诉我如何在MSSQL Server中连接到哪个数据库。

获取代码回报

As per @lad2025's comment, because the connection string has omitted the Database (or Initial Catalog ) definition, so the connection will default to the default database assigned to the login used to connect to the server ( sa in your case). 根据@ lad2025的注释,由于连接字符串省略了Database(或Initial Catalog )定义,因此该连接将默认为分配给用于连接服务器的登录名的默认数据库(在您的情况下为sa )。

There are a number of ways to find the database 有多种找到数据库的方法

  • As suggested in the comment, determine the default mapping for the user 如注释中所建议,为用户确定默认映射
  • Or, once your app has an active connection, run sp_who (as you already have sa access) and look up the dbname column for the connection. 或者,一旦您的应用程序具有活动的连接,请运行sp_who (因为您已经具有sa访问权限)并在dbname列中查找该连接。

You should however address this behaviour: 但是,您应该解决此问题:

  • defaulting the database on the server in this way is fragile, as eg the DBA could change the default database 以这种方式在服务器上默认数据库非常脆弱,例如,DBA可以更改默认数据库
  • Never let an app use sa as a login. 切勿让应用使用sa作为登录名。 The login should be restricted to the minimum set of accesses required 应将登录名限制为所需的最小访问权限集

You can check default database using SQL Server Management Studio : 您可以使用SQL Server Management Studio检查默认数据库:

Object Explorer -> Server -> Security -> Logins -> sa -> right click -> check default database

Something like: 就像是:

在此处输入图片说明

Image source: http://www.mytechmantra.com/LearnSQLServer/Fix-cannot-open-user-default-database-Login-failed-Login-failed-for-user-SQL-Server-Error/ 图片来源: http : //www.mytechmantra.com/LearnSQLServer/Fix-cannot-open-user-default-database-Login-failed-Login-failed-for-user-SQL-Server-Error/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM