gpt4 book ai didi

c# - DbContext 的 EF 5.x 代码优先连接字符串不起作用

转载 作者:行者123 更新时间:2023-11-30 23:22:22 32 4
gpt4 key购买 nike

我有一个包含 EF 代码优先类和数据库上下文的项目

public partial class MyProjectContext : DbContext
{
public MyProjectContext()
: base("name=MyProjectContext")
{
}

public DbSet<Person> Persons { get; set; }

我的 app.config 看起来像

<add name="MyProjectContext" connectionString="server=<serverIP>; user id=<userid>; password=<password>; database=<dbName>; pooling=false; Persist Security Info=True " providerName="MySql.Data.MySqlClient" />

我在我的 Web 项目中引用了上面的这个 Project1,我在 Web 项目中也有下面的连接字符串

<add name="MyProjectContext" connectionString="server=<serverIP>; user id=<userid>; password=<password>; database=<dbName>; pooling=false; Persist Security Info=True " providerName="MySql.Data.MySqlClient" />

这是我抛出错误的代码

MyProjectContext db = new MyProjectContext();
Person user = (from q in db. Persons
where q.PersonId == 1
select q).First();

我收到以下错误

{"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct."}

{"The provider did not return a ProviderManifestToken string."}

如果我首先将 MYsql 与模型一起使用,它可以与我拥有的连接器一起使用,它是 6.6.4.0我已经手动创建了一个数据库,所以我不想进行创建数据库的初始化。与必须运行 tt 文件和更新 edmx 文件相比,这段代码首先让我对类有了更多的控制。

最佳答案

您根本不需要输入 name=,只要包含 MyProjectContext 的项目还在其 App.Config 文件中包含该连接字符串:

public MyProjectContext() : base("MyProjectContext")

关于c# - DbContext 的 EF 5.x 代码优先连接字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943873/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com