具有多个 ASP.NET 站点的 SQL Express 用户实例
如何获得两个独立的 ASP.NET 站点(在本例中是使用实体框架和 Web 服务的网站)以使用相同的本地数据库 mdf 和 mdf 。 ldf?
我环顾四周,我认为这种本地数据库称为用户实例 - 至少我目前使用它的方式。 我不确定它是否有效,直到我从 MSDN Library,现在我真的不确定它是否会起作用。
用户实例场景包括:...
使用 Windows 身份验证的专用 ASP.NET 托管。 单个 SQL Server Express 实例可以托管在 Intranet 上。 应用程序使用 ASPNET Windows 帐户进行连接,而不是使用模拟。 用户实例不应用于第三方或共享托管方案,在这些方案中,所有应用程序将共享相同的用户实例,并且不再保持彼此隔离。
无需任何额外配置即可立即工作吗? 当我尝试向每个连续的应用程序添加数据库时,如果有其他东西正在使用该数据库,我会收到文件使用中错误,这使我相信我不能这样做。 但是,如果我停止使用数据库,然后添加它,然后启动它,它似乎工作正常。 有人可以向我保证,以这种方式使用它不会被损坏或遇到任何错误吗?
优选地,mdf和mdf ldf 将存储在网站的 App_Data 目录 (|DataDirectory|) 中。 如果我这样做,我认为我无法可靠地使用服务的相对寻址,除非我将其放在站点的子目录中,这是正确的吗?
编辑:我使用 SQL Server Express 因为这是学校的作业,所以我几乎必须使用它。 如果有一种不需要用户实例的方法,我会尝试不使用它。
How can I get two separate ASP.NET sites, in this case a Web Site using Entity Framework and Web Service, to use the same local database mdf & ldf?
I looked around and I think that this kind of local DB is called a User Instance - at least they way I'm currently using it. I wasn't sure if it would work or not until I found the below info from the MSDN Library and now I'm really not sure if it will work.
User instance scenarios include:
...
Dedicated ASP.NET hosting using Windows Authentication. A single SQL Server Express instance can be hosted on an intranet. The application connects using the ASPNET Windows account, not by using impersonation. User instances should not be used for third-party or shared hosting scenarios where all applications would share the same user instance and would no longer remain isolated from each other.
Will this work right away without any extra configuration? When I try to add a DB to every successive application, I get a file in use error if anything else is using the DB which leads me to believe that I can't do it this way. However, if I stop the DB from being used, then add it, and then start it up, it seems to work fine. Does anyone have any reassurance for me that it won't be corrupted or experience any errors by using it this way?
Preferably, the mdf & ldf would be stored in the Web Site's App_Data directory (|DataDirectory|). If I do it this way, I don't think I can reliably use relative addressing for the Service unless I put it in a sub directory for the Site, is this correct?
edit: I'm using SQL Server Express because this is an assignment for school, so I pretty much have to use it. If there is a way without User Instance I'll try to do without it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能为此使用
用户实例
。 这一次只允许一个客户端连接到您的数据库。 当您的两个应用程序使用相同的凭据时,您会收到您所描述的错误。 当您的两个应用程序使用不同的凭据时,您实际上会获得两个单独的实例(也不是您想要的)。此外,用户实例仅在 SQL Server Express 上受支持。 我不知道你的生产环境是什么样的,但它可能不能在 SQL Server Express 上运行?
您应该在 Web 应用程序的连接字符串中使用
AttachDBFilename
来指向本地数据库:AttachDBFilename=|DataDirectory|\MyDB.mdf
。在您的 Web 服务中,您还可以使用
AttachDBFilename
但它取决于将其指向的 Web 服务的部署位置。You can't use
User Instance
for this. This allows just one client at a time to connect to your database. When your two applications use the same credentials, you get the error you describe. When your two applications use different credentials, you essentially get two separate instances (also not what you want).Besides, user instances are only supported on SQL Server Express. I don't know what your production environment looks like, but it probably does not run on SQL Server Express?
You should use
AttachDBFilename
in the connection string of your web application to point to the local database:AttachDBFilename=|DataDirectory|\MyDB.mdf
.In your web service you also use
AttachDBFilename
but it depends on the deployment location of the web service where to point it to.