简体   繁体   English

使用VS2012数据库项目:无法打开登录请求的数据库“ my_database”

[英]Using VS2012 Database Projects: Cannot open database “my_database” requested by the login

I'm trying to get a VS2012 Database Project up and running for an existing product. 我正在尝试为现有产品启动并运行VS2012数据库项目。 I schema compared my database to populate my new database project and everything looked good. 我对数据库进行了比较,以填充新的数据库项目,并且一切看起来都很好。 It appears to deploy fine with Visual Studio, no errors, but when I try to run my app against the freshly deployed database I get the following error: 使用Visual Studio似乎可以很好地部署,没有错误,但是当我尝试对新部署的数据库运行应用程序时,出现以下错误:

Cannot open database "my_database" requested by the login. 无法打开登录请求的数据库“ my_database”。 The login failed. 登录失败。

I've looked into this extensively and found some bizzare behavior I don't understand. 我对此进行了广泛研究,发现一些我不理解的奇怪行为。 If I execute the following sql after deploying my database, everything starts working: 如果我在部署数据库后执行以下sql,则一切开始正常工作:

DROP SCHEMA [my_app_user] 
GO
DROP USER [my_app_user] 
GO
CREATE SCHEMA [my_app_user]
GO
CREATE USER [my_app_user] FOR LOGIN [my_app_user] WITH DEFAULT_SCHEMA=[my_app_user]
GO
ALTER ROLE [my_app_role] ADD MEMBER [my_app_user];
GO

The odd thing is, this code is in my database project too, exactly as shown sans the drop statements. 奇怪的是,该代码也在我的数据库项目中,完全与drop语句相同。 It creates all these things when I deploy but it does not work. 当我部署时,它会创建所有这些东西,但是它不起作用。 Another odd behavior is when I open the login for the user in Sql Server Management Studio 2012 and look at the User Mapping for the login, it's not mapped. 另一个奇怪的行为是,当我在Sql Server Management Studio 2012中为用户打开登录名并查看该登录名的“用户映射”时,该用户名未映射。 I can check the box for my database and it all looks good, but when I click ok and go back into that same window, it's not checked again. 我可以选中数据库的复选框,一切看起来都很好,但是当我单击“确定”并返回到同一窗口时,就不再选中它。 Running the above script makes the User Mapping window look good. 运行上述脚本可使“用户映射”窗口看起来不错。

UPDATE: I moved the create statements for login/schema/user and the alter statement for role into the post deploy scripted used by the database project and now it's publishes to the database just fine. 更新:我将login / schema / user的create语句和角色的alter语句移到了数据库项目使用的post deploy脚本中,现在可以将其发布到数据库了。 The Database Projects, however, seem to want to put these statements in with the rest of the schema, as opposed to post deploy, so I'd still like to know what, if anything, I can do to resolve this weirdness.. 但是,数据库项目似乎希望将这些语句与架构的其余部分放在一起,而不是后期部署,因此我仍然想知道我可以做些什么来解决这种怪异的事情。

What is going on here and what can I do to fix this? 这是怎么回事,我该怎么解决? Thanks for any help!! 谢谢你的帮助!!

You need to associated the user to the role in your post deployment scripts: 您需要将用户与后期部署脚本中的角色相关联:

http://msdn.microsoft.com/en-us/library/aa833199(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/aa833199(v=vs.100).aspx

This sounds like a permissions issue. 这听起来像是权限问题。 Does the security context that the database is deployed with have the permissions to run this sql statement? 数据库所部署的安全上下文是否具有运行此sql语句的权限?

DROP SCHEMA [my_app_user] 
GO
DROP USER [my_app_user] 
GO
CREATE SCHEMA [my_app_user]
GO
CREATE USER [my_app_user] FOR LOGIN [my_app_user] WITH DEFAULT_SCHEMA=[my_app_user]
GO
ALTER ROLE [my_app_role] ADD MEMBER [my_app_user];
GO

Take a peek in the SQL Server logs for the point in time when the database is being deployed, and also look in the application log on the box that does the deployment to see if there is a more detailed error message. 观察一下SQL Server日志中部署数据库的时间点,并查看进行部署的框上的应用程序日志,以查看是否有更详细的错误消息。

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

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