简体   繁体   English

无法通过IIS访问SQL Server

[英]Can't access SQL Server via IIS

I want to run my ASP.NET MVC application on the local IIS 8 server but I can't get access to SQL Server 2014. Both IIS and SQL Server run on the same host and I am using Windows 8. This is what I have done so far: 我想在本地IIS 8服务器上运行ASP.NET MVC应用程序,但无法访问SQL Server2014。IIS和SQL Server都在同一主机上运行,​​并且我使用的是Windows8。这就是我拥有的到目前为止:

In the application I created a model called Employee : 在应用程序中,我创建了一个名为Employee的模型:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I want to connect to the database with using Entity Framework so I created another class called EmployeeDbContext : 我想使用实体框架连接到数据库,所以我创建了另一个名为EmployeeDbContext类:

public class EmployeeDbContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}

I have only one Home controller: 我只有一个Home控制器:

public string Index()
{
    EmployeeDbContext employeeDbContext = new EmployeeDbContext();
    return "Hello world";
}

I am using SQL Server 2014 Express engine with Windows Authentication and the name of my computer is ZSÓTÉ. 我正在使用带有Windows身份验证的SQL Server 2014 Express引擎,我的计算机名为ZSÓTÉ。 Here is the connection string from the web.config : 这是来自web.config的连接字符串:

<add name="EmployeeDbContext" 
     connectionString="Server=ZSÓTÉ\SQLEXPRESS;Initial Catalog=Test;Integrated Security=SSPI" 
     providerName="System.Data.SqlClient" />

In the Global.asax I didn't set any initializer strategy so by default if the Test database doesn't exist, then the application should create it automatically. Global.asax我没有设置任何初始化策略,因此默认情况下,如果Test数据库不存在,则应用程序应自动创建它。

After that I set on the properties window to use IIS Local Server and I create a virtual directory for the application. 之后,我在属性窗口上设置为使用IIS Local Server,然后为该应用程序创建一个虚拟目录。 So far everything is ok, I can see the application in the IIS manager and I set to run under the DefaultAppPool . 到目前为止,一切正常,我可以在IIS管理器中看到该应用程序,并设置为在DefaultAppPool下运行。 After that I set all the permissions of the project folder for the IIS APPPOOL\\DefaultAppPool object. 之后,我为IIS APPPOOL\\DefaultAppPool对象设置了项目文件夹的所有权限。 The settings are fine I can access the application from the IIS server perfectly. 设置很好,我可以完美地从IIS服务器访问应用程序。

Finally I created a login for IIS APPPOOL\\DefaultAppPool in SQL Server Management Studio and I set these two roles: public and dbcreator . 最后,我在SQL Server Management Studio中为IIS APPPOOL\\DefaultAppPool创建了登录名,并设置了这两个角色: publicdbcreator But even after all these setups the application doesn't work correctly via IIS. 但是即使完成所有这些设置,应用程序也无法通过IIS正常运行。

Though I get the "Hello world" message on the browser, but the Test database is never created. 尽管我在浏览器上收到“ Hello world”消息,但是从未创建Test数据库。 The strangest thing is if I make some malicious change in the connection string, I don't even get any compilation error, just the "Hello world" message in the browser. 最奇怪的是,如果我对连接字符串进行了恶意更改,我什至没有得到任何编译错误,只是浏览器中出现了“ Hello world”消息。

What am I doing wrong? 我究竟做错了什么? Thank you in advance! 先感谢您!

The database is created the first time you access to entities. 该数据库是在您首次访问实体时创建的。
Try to do this: 尝试这样做:

public string Index()
{
    EmployeeDbContext employeeDbContext = new EmployeeDbContext();
    employeeDbContext.Employees.ToList();
    return "Hello world";
}

At this point you will receive an error if db creation does not work. 此时,如果数据库创建不起作用,您将收到一个错误。

Next steps if something still does not work: 如果仍然无法解决问题,请执行以下步骤:
In the connection string use .\\SQLEXPRESS because could happen everything to accents. 在连接字符串中使用。\\ SQLEXPRESS,因为重音可能会发生。
Assign to the user in SQL Server system administration role. 分配给具有SQL Server系统管理角色的用户。 If it work you then start to reduce rights (or, better, change approach). 如果可行,那么您将开始减少权利(或者更好的是更改方法)。

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

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