简体   繁体   English

在ASP.NET MVC应用程序中管理多个客户数据库

[英]Managing multiple customer databases in ASP.NET MVC application

I am building an application that requires separate SQL Server databases for each customer. 我正在构建一个需要为每个客户单独的SQL Server数据库的应用程序。

To achieve this, I need to be able to create a new customer folder, put a copy of a prototype database in the folder, change the name of the database, and attach it as a new "database instance" to SQL Server. 为此,我需要能够创建一个新的客户文件夹,在该文件夹中放置一个原型数据库的副本,更改数据库的名称,并将其作为新的“数据库实例”附加到SQL Server。 The prototype database contains all of the required table, field and index definitions, but no data records. 原型数据库包含所有必需的表,字段和索引定义,但没有数据记录。 I will be using SMO to manage attaching, detaching and renaming the databases. 我将使用SMO来管理数据库的附加,分离和重命名。

In the process of creating the prototype database, I tried attaching a copy of the database (companion .MDF, .LDF pair) to SQL Server, using Sql Server Management Studio, and discovered that SSMS expects the database to reside in 在创建原型数据库的过程中,我尝试使用Sql Server Management Studio将数据库副本(伴侣.MDF,.LDF对)附加到SQL Server,并发现SSMS希望数据库驻留在其中。

c:\program files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDatabaseName.MDF

Is this a "feature" of SQL Server? 这是SQL Server的“功能”吗? Is there a way to manage individual databases in separate directories? 有没有办法在单独的目录中管理各个数据库? Or am I going to have to put all of the customer databases in the same directory? 还是我必须将所有客户数据库放在同一目录中? (I was hoping for a little better control than this). (我希望比这更好的控制)。

NOTE: I am currently using SQL Server Express, but for testing purposes only. 注意:我当前正在使用SQL Server Express,但仅​​用于测试目的。 The production database will be SQL Server 2008, Enterprise version. 生产数据库将是SQL Server 2008企业版。 So "User Instances" are not an option. 因此,“用户实例”不是一个选择。

The MDF has in it the table containing the physical path of all the database files, as they were on the instance it was detached from. MDF中包含一个表,该表包含所有数据库文件的物理路径,就像它们在与之分离的实例上一样。 You can overwrite the location(s) during the attach operation: 您可以在附加操作期间覆盖位置:

CREATE DATABASE <dbname>
 ON (name=dbfilelogicalname, filename='c:\myNewPath\dbfilename.mdf'),
 (name=dbfile2logicalname, filename='c:\myNewPath\dbfilename2.ndf'),
 (name=dbloglogicalname, filename='c:\myNewPath\dblogfilename.ldf')
 FOR ATTACH;

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

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