简体   繁体   English

SQL Server Express开发人员设置-访问被拒绝

[英]SQL Server Express dev setup - access denied whack-a-mole

I have a development environment in which I need to use IIS and SQL Server Express. 我有一个开发环境,需要在其中使用IIS和SQL Server Express。 My connection string looks like this: 我的连接字符串如下所示:

<add name="DefaultConnection" 
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyProject.mdf;Integrated Security=True;User Instance=True" 
     providerName="System.Data.SqlClient" />

This works great when I run the app and browse to the site. 当我运行该应用程序并浏览到该站点时,这非常有用。 Data is returned from the database and I can log into Management Studio and view that data. 数据从数据库返回,我可以登录到Management Studio并查看该数据。 The problem is when I try to push a new data migration using Update-Database . 问题是当我尝试使用Update-Database进行新的数据迁移时。

I then get this error message: 然后,我收到此错误消息:

Login failed for user 'AzureAD\\MyAccount' 用户“ AzureAD \\ MyAccount”的登录失败

If I remove User Instance=True from my connection string, the Update-Database command suddenly works! 如果我从连接字符串中删除User Instance=True ,则Update-Database命令突然起作用! Then I refresh my page and see the following error from all endpoints that require the database: 然后刷新页面,并从需要数据库的所有端点中看到以下错误:

CREATE DATABASE permission denied in database 'master'. CREATE DATABASE权限在数据库“ master”中被拒绝。

I have already tried the trick of deleting this folder. 我已经尝试了删除该文件夹的技巧。 It did not solve it. 它没有解决。

C:\Users\MyAccount\AppData\Local\Microsoft\Microsoft SQL Server Data

What gives? 是什么赋予了?

Figured this out so Ill leave the answer here for the next dev: 弄清楚了,所以我在这里留下下一个开发者的答案:

As I mentioned, my dev setup is running IIS and SQL Express. 如前所述,我的开发人员设置正在运行IIS和SQL Express。

  1. Update the connection string to point to an Initial Catalog=MyDatabase instead of the AttachDbFilename=|DataDirectory|\\MyProject.mdf 更新连接字符串以指向“ Initial Catalog=MyDatabase而不是AttachDbFilename=|DataDirectory|\\MyProject.mdf
  2. Remove User Instance=True . 删除User Instance=True Your connection string now looks like: 现在,您的连接字符串如下所示:

Data Source=.\\SQLEXPRESS;Initial Catalog=FullStackFitness;Integrated Security=True

  1. Create the database by running Update-Database . 通过运行Update-Database创建Update-Database
  2. Create a SQL login for the app pool account (IIS APPPOOL\\ACCOUNT) dbcreator and sysadmin on the database. 在数据库上为应用程序池帐户(IIS APPPOOL \\ ACCOUNT)dbcreator和sysadmin创建一个SQL登录名。

I did not test it but I believe you can keep using an mdf file and still get this to work by running only steps 2 - 4. 我没有对其进行测试,但是我相信您可以继续使用mdf文件,并且只需运行第2-4步,即可继续使用该文件。

Troubleshooting Tips 故障排除技巧

Help troubleshooting the access denied issue came from a tip on Scott Allens blog : 有关对访问被拒绝问题进行故障排除的帮助,来自Scott Allens 博客上的提示:

The first step I would recommend is trying to determine what connection string the framework is using, because the exception doesn't tell you the connection string, and the connection string can be controlled by a variety of conventions, configurations, and code. 我建议的第一步是尝试确定框架使用的连接字符串,因为异常不会告诉您连接字符串,并且可以通过多种约定,配置和代码来控制连接字符串。

To find out the connection string, I'd add some logging to a default constructor in my DbContext derived class. 为了找出连接字符串,我将一些日志记录添加到DbContext派生类中的默认构造函数中。

public class DepartmentDb : DbContext 
{
    public DepartmentDb() 
    {
       Debug.Write(Database.Connection.ConnectionString);
    }

    public DbSet<Person> People { get; set; }
 }

Run the application with the debugger and watch the Visual Studio Output window. 使用调试器运行该应用程序,并查看“ Visual Studio输出”窗口。 Or, set a breakpoint and observe the ConnectionString property as you go somewhere in the application that tries to make a database connection. 或者,在尝试建立数据库连接的应用程序中的某个位置,设置一个断点并观察ConnectionString属性。

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

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