简体   繁体   English

Visual Studio 2010 - 导入数据库的数据库项目无法构建

[英]Visual Studio 2010 - Database Project with imported database fails to build

I'm looking at the Database Project in VS2010, the idea being that I want something I can use to keep track of the database schema (in source control) and the ability to generate "new install" scripts, and change scripts.我正在查看 VS2010 中的数据库项目,我的想法是我想要一些可以用来跟踪数据库模式(在源代码控制中)以及生成“新安装”脚本和更改脚本的能力。

When I create a new database project wizard and import my existing database schema, it won't "build".当我创建一个新的数据库项目向导并导入我现有的数据库模式时,它不会“构建”。 I get the error:我得到错误:

SQL03006: User: [scanner] has an unresolved reference to Login [scanner]. SQL03006:用户:[scanner] 对 Login [scanner] 的引用未解析。

The SQL that generates this error:产生此错误的 SQL:

CREATE USER [scanner] FOR LOGIN [scanner]; CREATE USER [scanner] FOR LOGIN [scanner];

The user "scanner" is a login defined in the database I imported.用户“scanner”是我导入的数据库中定义的登录名。 I have no idea what it's teling me, and google isn't throwing much up.我不知道它在告诉我什么,谷歌也没有吐太多。 Any ideas?有任何想法吗?

The Login is actually defined in the master database of the server install. Login实际上是在服务器安装的master数据库中定义的。 The CREATE USER statement needs to point at an existing Login otherwise it errors. CREATE USER语句需要指向现有的Login,否则会出错。 The Database Project is not aware of the Login at the server level. 数据库项目不知道服务器级别的登录。 Either you can create a Server Project to handle the Logins, or you can turn off the checking of Security objects in your Database Project. 您可以创建服务器项目来处理登录,也可以关闭数据库项目中安全对象的检查。 See the answer by Demetri M for more details: http://social.msdn.microsoft.com/Forums/eu/vstsdb/thread/1f8226fe-b791-4344-8735-3d38307e8664 有关更多详细信息,请参阅Demetri M的答案: http//social.msdn.microsoft.com/Forums/eu/vstsdb/thread/1f8226fe-b791-4344-8735-3d38307e8664

I'm using Visual Studio 2012 for a SQL Server 2008 R2 database project. 我正在使用Visual Studio 2012进行SQL Server 2008 R2数据库项目。 The options listed by @Judah don't appear to work anymore. @Judah列出的选项似乎不再起作用。

They now appear to be Settings that you configure while doing a Schema Compare: 现在,它们似乎是您在执行架构比较时配置的设置:

Right click database project > Choose 'Schema Compare' > Choose the 'Settings' gear icon > Choose the 'Object Types' tab > 右键单击数据库项目>选择“架构比较”>选择“设置”齿轮图标>选择“对象类型”选项卡>

Logins are Non-Application-scoped. 登录是非应用程序范围的。 Database roles, Permissions, Role Memberships, and Users are all Application-scoped. 数据库角色,权限,角色成员身份和用户都是应用程序范围的。

Unfortunately, the only way that I can find to preserve these is to save the schema compare. 不幸的是,我能找到保留这些的唯一方法是保存模式比较。 This can be a little inconvenient if you're sharing this on a team and would like project/database (or server) settings for any schema compare. 如果您在团队中共享此内容并希望任何架构比较的项目/数据库(或服务器)设置,这可能会有点不方便。

It gets the job done, though. 不过,它完成了工作。

You can change the create user scripts to create roles. 您可以更改创建用户脚本以创建角色。 So instead of "Create user userName for login loginName;" 因此,而不是“为登录loginName创建用户userName;”

use "Create Role [userName] authorization dbo;" 使用“创建角色[userName]授权dbo;”

This is a hack, but as long as you aren't having to deal with users and roles in your project, you can happily do the following: 这是一个黑客,但只要您不必处理项目中的用户和角色,您就可以愉快地执行以下操作:

GRANT EXECUTE ON OBJECT::[dbo].[sp_name] TO [userName]; 在对象上执行GRANT EXECUTE :: [dbo]。[sp_name] TO [userName];

Apparently, this issue is still occurring on VS2017 database projects as well. 显然,这个问题在VS2017数据库项目中仍然存在。

I've managed to solve it by first creating the login and then create the user. 我设法通过首先创建登录然后创建用户来解决它。

    -- Windows Account
    CREATE LOGIN [Domain\Username]
    FROM WINDOWS WITH DEFAULT_LANGUAGE = [us_english];

    GO

    CREATE USER [Domain\Username] FOR LOGIN [Domain\Username];
    GO

    -- Sql Acccount

    CREATE LOGIN [sql_account] WITH PASSWORD = 'Ch@ngeth1spA$swurD'
    GO

    CREATE USER [sql_account]
    FROM LOGIN [sql_account]
    WITH DEFAULT_SCHEMA = dbo

    GO



    -- Then set the sql file Build Action to "Build"

In the database project open the 'Security' folder (assuming that's how your database was imported).在数据库项目中打开“Security”文件夹(假设您的数据库是这样导入的)。 For each user profile that is causing an issue, set the build action to 'None' in the properties panel.对于导致问题的每个用户配置文件,在属性面板中将构建操作设置为“无”。 You will also have to remove them from any other files in which they appear, including Permissions.sql and RoleMemberships.sql.您还必须从它们出现的任何其他文件中删除它们,包括 Permissions.sql 和 RoleMemberships.sql。

在此处输入图像描述

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

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