简体   繁体   English

如何使SQL数据库用户和角色进入Visual Studio中的MVC EF项目?

[英]How do I get SQL database users & roles into my MVC EF project in Visual Studio?

I was able to create the SQL database tables using "schema compare" in visual studio. 我可以在Visual Studio中使用“模式比较”来创建SQL数据库表。 I added the login & user to the database directly using T-SQL, and also used T-SQL to add the user to datareader & datawriter roles. 我直接使用T-SQL将登录名和用户添加到数据库,还使用T-SQL将用户添加到数据读取器和数据写入器角色。

Now, when I do a "schema compare" in visual studio using my project as the source, it wants to delete the user and roles from the database. 现在,当我使用项目作为源在Visual Studio中进行“架构比较”时,它想从数据库中删除用户和角色。 How do I add these to the project? 如何将它们添加到项目中?

It depends if you are using the new Sql Server Data Tools (SSDT) project (.sqlproj) or the older 2010 SQL Database Project (.dbproj). 这取决于您使用的是新的SQL Server数据工具(SSDT)项目(.sqlproj)还是旧的2010 SQL数据库项目(.dbproj)。

If you are using the new SSDT project - then you just add the objects into the project: 如果您正在使用新的SSDT项目-那么只需将对象添加到项目中:

  • Create a file for your new Login 为您的新登录名创建文件
  • Create a file for your new DB User 为新的数据库用户创建文件
  • Create a file called YourDatabaseName.rolememberships.sql 创建一个名为YourDatabaseName.rolememberships.sql的文件

That file will have code in it like: 该文件中将包含以下代码:

EXECUTE sp_addrolemember @rolename = N'db_datareader', @membername = N'NewDBUser';

And you will need a Permissions.sql file. 您将需要一个Permissions.sql文件。 In there you will grant the Login connect rights like this: 在其中,您将授予Login连接权限,如下所示:

GRANT CONNECT TO [NewLogin] AS [dbo];

If however you are using the old database project you will need to create a new project in your solution called a Server project. 但是,如果您正在使用旧的数据库项目,则需要在解决方案中创建一个称为服务器项目的新项目。 It looks very much like your database project, except it is of the master database. 它看起来非常像您的数据库项目,但它属于master数据库。

You do similar things in this new server project as I outlined above and in your original database project you will need to add the server project as a reference. 您在我上面概述的这个新服务器项目中执行了类似的操作,而在原始数据库项目中,您将需要添加服务器项目作为参考。


A caveat to this whole process. 对整个过程的警告。 You will want to add the Login with the SID value so that you don't end up with orphaned user scenarios when you try and put a backup of your database on some other server. 您将要添加带有SID值的Login,以便在尝试将数据库备份放在其他服务器上时不会遇到孤立的用户情况。

I have used the process outlined in this MS KB to get the SID in the first place and then if you refer to the BOL section about CREATE LOGIN you will see what the syntax should be for creating your login file. 我首先使用了此MS KB中概述的过程来获取SID,然后,如果您参考有关CREATE LOGIN的BOL部分,您将看到用于创建登录文件的语法。

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

相关问题 如何列出MVC用户及其角色? - How do I list MVC users and their roles? 如何在Visual Studio 2010中使用我的设置和部署项目部署SQL Server数据库? - How can I deploy the SQL server database with my setup and deployment project in Visual Studio 2010? 我如何获得Visual Studio解决方案来构建MVC项目并将其运行到另一个文件夹? - How do i get a Visual Studio solution to build and run an MVC project to another folder? 如何在Visual Studio中正确显示MVC项目? - How do I make a MVC project appear properly in Visual Studio? 如何让我的存储过程在我的 Visual Studio 项目中运行? - How do I get my stored procedure to work inside my Visual Studio project? 如何使用EF和SQL管理ASP MVC中的角色 - How to manage roles in ASP MVC with EF and SQL 如何在 Visual Studio 2022 中使用已连接的 SQL 服务器数据库进行查询? - How do I make a query using my connected SQL Server Database in Visual Studio 2022? 更新数据库源后,如何让 Visual Studio 2010 刷新我的数据集? - How do I get Visual Studio 2010 to refresh my DataSet after I have updated the Database source? 如何使用Visual Studio项目中包含的文件? - How do I use a file included in my visual studio project? 如何在Visual Studio中获得通往另一个项目的路径? - How Do I Get The Path To Another Project In Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM