连接到数据库时出现问题 - 用户实例和实体框架问题

发布于 2024-12-04 05:05:53 字数 2082 浏览 2 评论 0原文

我在我的 asp.net 应用程序中基于 .mdf 文件创建了一个实体模型文件 (.edmx),并将其放置在我的 App_Data 文件夹中。

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

<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 文件中(我不确定这是否有必要)。

当尝试稍后在我的计算机上运行相同的应用程序时,我遇到了两个问题(我已在另一台计算机上创建了该应用程序)。

第一个是关于用户实例:

由于启动用户实例进程失败,无法生成 SQL Server 的用户实例。连接将被关闭

我真正理解的第一件事是用户实例实际定义的内容。

根据 MSDN :

用户实例是由父实例生成的 SQL Server Express 数据库引擎的单独实例

我的本地数据库是否被视为用户实例?

我真的很想对此事进行一些澄清,因为我发现帖子建议 将其标记为 False,但我不清楚原因。

关于此事的另一篇文章< /a>

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

修复

我已经完成了该帖子中解释的操作

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

尝试为文件附加自动命名数据库(我的 app_data\my.mdf 文件的完整路径)存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。< /p>

我已经尝试删除所有文件

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

并按照其他帖子中的建议重新启动,但我仍然找不到此问题的明确解决方案。

我知道这是一篇很长的文章,但如果有任何帮助,我们将不胜感激

,提前致谢。

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="c:\users\ext\documents\visual studio 2010\Projects\WebProject_A\WebProject_A\App_Data\Sales_DB.mdf";integrated security=True;USER INSTANCE=TRUE;multipleactiveresultsets=True;App=EntityFramework'" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

The above is placed in an app.config file created by the wizard.

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).

The first was regarding the user instance:

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

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

According to MSDN :

A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance

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 post on this matter

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.

The Fix

I've done what was explained in that post

but now I've come across a new problem :

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.

I've already attempted to delete all the files under

 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.

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

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-12-11 05:05:53

阅读这篇关于用户实例到底是什么的非常好的概述

简而言之:

  • SQL Server 允许您在一台计算机上拥有其自身的多个实例 - 只需使用主机的计算机名称(或 IP 地址)即可访问默认实例 -所有其他实例都需要通过实例名称指定(例如您计算机上的SQLExpress

  • 出于开发目的,您还可以创建一个用户实例 - 每个用户都有自己单独的 SQL Server 实例(仅适用于Express 版本)并附加一个数据库按其文件名(path\Sales_DB.mdf) 到该用户实例。该实例按需启动,然后运行,并在不再需要时关闭

虽然这非常适合开发,但它并不是真正适合以后使用 - 当然不适合生产使用。

在生产环境中,您希望将数据库文件附加到它们应运行的实际 SQL Server 实例 - 通常使用 SQL Server Management Studio。执行此操作时,您不再通过其文件名引用该数据库,而是使用连接到服务器时为数据库指定的数据库名称 。在这种情况下,SQL Server 将处理操作的所有细节 - 并且 SQL Server 实例始终启动并运行(不需要始终“按需”启动),并且您可以使用诸如权限和登录来控制对数据库的访问。

然后,您的连接字符串将如下所示:

<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 将负责处理。您只需通过数据库名称引用数据库即可。

Read this very good overview of what user instances really are.

In a nutshell:

  • 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)

  • 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.

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>

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.

过气美图社 2024-12-11 05:05:53

解决方案总是比预期更简单:

  1. 双击 model.edmx 文件 ->设计师将打开。
  2. 右键单击设计器-> 从数据库更新模型
  3. 新连接-> 在类型下选择数据库文件 (.mdf)
  4. 浏览->转到您的 App_Data 文件夹,然后选择数据库。
  5. App.config 中创建的连接字符串复制到 web.config

再见。

The solution is always simpler then anticipated:

  1. double click the model.edmx file -> the designer will open.
  2. right click on the designer -> Update model from database .
  3. new connection -> Choose Database file (.mdf) under type .
  4. Browse -> go to your App_Data folder, and choose the DB.
  5. copy the connection string created in App.config to web.config.

bye.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文