问题连接到数据库 - 用户实例和Entity Framework问题

问题描述:

我创建了一个实体模型文件(的.edmx)根据我在asp.net应用程序,放在我的的App_Data ​​ code>文件夹中的.mdf文件。

I've created an entity model file (.edmx) based on an .mdf file in my asp.net application, placed in my App_Data Folder.

首先我连接字符串,由实体框架向导创建:

first of all my connection string, created by the entity framework wizard :

<connectionStrings>
    <add name="Sales_DBEntities" 
        connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;attachdbfilename=&quot;c:\users\ext\documents\visual studio 2010\Projects\WebProject_A\WebProject_A\App_Data\Sales_DB.mdf&quot;;integrated security=True;USER INSTANCE=TRUE;multipleactiveresultsets=True;App=EntityFramework'" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

以上是放置在由向导创建一个的app.config 文件。

我已经复制相同的连接字符串的 web.config文件,以及(我不知道这是必要的)。

I've copied the same connection string to the web.config file as well (I'm not sure if this is necessary).

在尝试之后运行我的机器上相同的应用程序时,我遇到两个问题(我创建了一个不同的机器上的应用程序)。

I've come across two problems when attempting to later run the same application on a my machine (I've created the application on a different machine).

首先是关于用户实例:

无法生成SQL Server的用户实例由于开始为用户实例的进程失败。该连接将被关闭。

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed

的第一件事情,我真的不COM prehend是用户实例实际上定义了。

The first thing I don't really comprehend is what a user instance actually defines.

根据MSDN:

用户实例是由父实例生成的SQL服务器前preSS数据库引擎的单独实例

是我的本地数据库考虑用户实例?

Is my local database considered a user instance ?

我真的很喜欢一些澄清此事,因为我碰到的帖子暗示来到
将其标记为假,原因是我也不清楚。

I would really like some clarification on the matter because I came across posts suggesting to mark it as False, and the reason was not clear to me .

another张贴在这个问题上

从那里,我遇到另一种解决方案,它解释说,对于这个问题,你需要来
你的机器上的SQL Server中启用用户实例。

From there I came across another solution that explains that for this problem you need to enable user instance in the SQL Server on your machine.

的修复

我所做的事,是在该职位说明

I've done what was explained in that post

但现在我遇到了一个新问题:

but now I've come across a new problem :

要(完整路径到我的App_Data \\ my.mdf文件)具有相同的名称附加自动命名的数据库文件数据库的尝试是否存在,或者指定的文件无法打开,或它位于UNC共享。

An attempt to attach an auto-named database for file (Full Path to my app_data\my.mdf file) A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

我已经试图删除在

 C:\Documents and Settings\(your user account name)\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS

和重启一样建议在其他职位,但我仍然无法找到问题的一个明确的解决方案。

and rebooting like suggested in other posts, but still I can't find a clear solution for this problem.

我知道这是一个很长的文章,但任何帮助,将AP preciated

I know this is a long post but any help would be appreciated

在此先感谢。

阅读本的很好的哪些用户实例真的是概述。

在一言以蔽之:


  • SQL Server允许您有一台计算机上的多个自身的实例 - 默认实例可以仅使用机器名(或IP地址)的主机的访问 - 所有其他情况下需要由一个实例名指定(如 SQLEx preSS 你的机器上)

  • SQL Server allows you to have multiple instances of itself on a single computer - the default instance can be accessed using just the machine name (or IP address) of the host machine - all other instances need to be specified by an instance name (like the SQLExpress on your machine)

为发展宗旨,你也可以创建一个用户实例 - 每个用户得到的SQL Server自己单独的实例(在防爆preSS版工程STRONG>只)和数据库是由它的文件名(路径\\ Sales_DB.mdf )到该用户实例连接。这个实例启动时需求,然后运行和关闭时不再需要的

for development purposes, you can also create a user instance - each user gets their own separate instance of SQL Server (works in the Express edition only) and a database is attached by its file name (path\Sales_DB.mdf) to that user instance. This instance is started up on demand, then runs and is shut down when no longer needed

虽然这种发展的伟大工程,它不是真的打算以后 - 当然不是在生产中使用

While this works great for development, it's not really intended for later on - certainly not for production use.

在生产环境中,你想的附加数据库文件,以实际的SQL Server实例,他们应该运行 - 通常使用SQL Server Management Studio中。当你这样做,你不再通过它的文件名指的是数据库,而是你使用数据库名称这是给所附加的数据库服务器时。在这种情况下,SQL Server将处理业务的所有细节 - 这SQL Server实例启动并运行所有的时间(并不需要按需所有的时间来启动),你可以使用之类的东西权限和登录控制对数据库的访问。

In a production environment, you want to attach your database files to the actual SQL Server instance they should run on - typically using SQL Server Management Studio. When you do this, you no longer refer to that database via its file name, but instead you use the database name that was given to the database when being attached to the server. In this case, SQL Server will handle all details of operations - and that SQL Server instance is up and running all the time (doesn't need to be started "on demand" all the time), and you can use things like permissions and logins to control access to the database.

您的连接字符串,然后会是这个样子:

Your connection string would then look something like this:

<connectionStrings>
    <add name="Sales_DBEntities" 
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

当你附加数据库到生产(或测试)SQL Server实例,你并不需要知道这些文件是什么构成的数据库,你不需要指定这些文件 - SQL Server将采取照顾。你只要通过其数据库名称引用数据库

When you have attached a database to a production (or testing) SQL Server instance, you do not need to know what the files are that make up the database, and you don't need to specify those files - SQL Server will take care of that. You just refer to the database via its database name.