EF Code First - 提供程序未返回 ProviderManifestToken 字符串

发布于 2024-12-05 05:50:12 字数 1234 浏览 6 评论 0原文

我试图运行一个非常简单的 EF Code First 示例,但遇到了上述问题。我遵循了这里的建议(如何为 EF Code First 配置 ProviderManifestToken )但无济于事。我最终设法让 EF 通过将实际连接字符串传递到 DBContext 而不是我定义的连接字符串名称来创建我的数据库和表。

以下是我最初在 app.config 中通过连接字符串定义的

<connectionStrings>
    <add name="ProductContext" 
         connectionString="integrated security=SSPI;
                           data source=MYMACHINE;
                           persist security info=False;
                           initial catalog=Product"
         providerName="System.Data.SqlClient" />
</connectionStrings>

名称“ProductContext”与类 ProductContext 匹配,并且数据库 Product 不存在。

按照上一个线程的建议,我将连接字符串名称传递给 ProductContext 和基本 DBContext cstor。这也不起作用。

最后,我传递了上面的连接字符串,一切正常,创建了数据库和表。

我正在使用 SQL Server 2008 和 EF 4.1。关于我做错了什么有什么想法吗?

谢谢,

Ken

更新

该应用程序是 WPF 应用程序,而不是 Web 应用程序。从 app.config 中删除连接字符串后,我得到了相同的异常:

“发生网络相关或特定于实例的错误 建立与 SQL Server 的连接。找不到服务器或 无法访问。验证实例名称是否正确 SQL Server 配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 定位服务器/实例时出错 指定)”

I'm trying to get a very simply EF Code First example running but ran into the above problem. I've followed the advice here ( How to configure ProviderManifestToken for EF Code First ) but to no avail. I finally managed to get EF to create my database and tables by passing the actual connection string to the DBContext instead of the connection string name I had defined.

Here is how I defined by connection string initially in app.config

<connectionStrings>
    <add name="ProductContext" 
         connectionString="integrated security=SSPI;
                           data source=MYMACHINE;
                           persist security info=False;
                           initial catalog=Product"
         providerName="System.Data.SqlClient" />
</connectionStrings>

The name "ProductContext" matches the class ProductContext and the database Product does not exist.

Following the advice on a previous thread I passed the connection string name to ProductContext and the base DBContext cstor. This did not work either.

Finally, I passed the connection string above instead and everything worked, the db was created and the tables.

I'm using SQL Server 2008 and EF 4.1. Any ideas as to what I'm doing wrong?

Thanks,

Ken

Update

The application is a WPF application, not a web application. I get the same exception after I remove the connectionstring from app.config:

"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)"

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

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

发布评论

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

评论(2

浅笑依然 2024-12-12 05:50:12

回答这个问题的问题在于,罪魁祸首可能不止一个。

也就是说,我发现有几件事需要检查:

  1. 您的数据源名称看起来无效。您需要声明主机名和实例名称。示例:.\SQLEXPRESS(注意 . 点)或 MyServerHostName\MySqlInstanceName

    使用 SQL Server Management Studio 验证您是否拥有有效的数据源,并首先在其中尝试。

  2. 权限。您正在使用集成安全性,因此进行数据库连接的应用程序的安全上下文必须具有适当的权限。检查事项:

    使用 SQL Server Mgmt Studio 连接到您的数据库。在左窗格(对象资源管理器)中选择您的数据库 -->数据库 -->选择您的数据库 -->安全性-->用户。确保您可以验证用于运行应用的帐户是否已被明确授予权限,或者可以通过成为组成员来继承这些权限。

当我遇到此错误时,我的问题被证明是我将 Integrated Security=SSPI 与“Username =”和“Password =”混合在一起......没有意识到我需要 Integrated Security=false;我的用户和密码参数被忽略。

对此不太确定:我确实读过一些人建议(至少为了故障排除)设置 Persist Security Info=true。

The problem with answering this question is that the culprit could be more than one issue.

That said, I see a couple things to check:

  1. Your Data Source name looks to be invalid. You need to declare the host name and the instance name. Example: .\SQLEXPRESS (notice the . dot) or MyServerHostName\MySqlInstanceName.

    Verify you have a valid data source by using SQL Server Management Studio and trying it there first.

  2. Permissions. You are using integrated security and so the security context of the application making the database connection must have proper rights. Things to check:

    Using SQL Server Mgmt Studio, connect to your database. Select your database in the left pane (Object Explorer) --> Databases --> Select your db --> Security --> Users. Make sure you can verify the account being used to run the app has either been granted rights explicitly or can inherit them by being a member of the groups.

When I had this error, my problem proved to be that I had mixed Integrated Security=SSPI with a "Username =" and "Password =" ...not realizing that I needed Integrated Security=false; my user and password parameters were being ignored.

Not sure about this one: I did read some people suggest (at least for troubleshooting) to set Persist Security Info=true.

深海蓝天 2024-12-12 05:50:12

我在 EF6 VS 2013 上遇到了类似的问题,当我在实体框架 - 首先反向工程代码 - 连接属性对话框上选择“保存密码”时,问题得到了解决。希望这对某人有帮助。

I had similar problem on EF6 VS 2013, the problem got solved, when I choose 'Save Password' on Entity Framework - Reverse Engineer Code First - Connection Properties Dialog. Hope this helps someone.

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