gpt4 book ai didi

c# - EF 代码优先 : Cannot connect to SQL Server

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:48 25 4
gpt4 key购买 nike

我正在尝试使用代码优先方法创建数据库。当我运行以下代码时,出现以下异常。我们如何克服这个问题?

注意事项:

  • 我的系统上有 SQL Server 2008 R2。
  • 我没有使用任何配置文件。我假设它将使用约定创建数据库
  • 还没有打开的连接;我刚刚重新启动系统并进行了测试。还是一样的问题。
  • EntityFramework.dll 的运行时版本是 v4.0.3xxx

异常:

The provider did not return a ProviderManifestToken string

留言:

This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.

内部异常:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

代码:

using System.Data.Entity;
namespace LijosEF
{
public class Dinner
{
public int DinnerID { get; set; }
public int Title { get; set; }
}

public class RSVP
{
public int RSVPID { get; set; }
public int DinnerID { get; set; }

public virtual Dinner Dinner { get; set; }
}

//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
}

namespace LijosEF
{
class Program
{
static void Main(string[] args)
{
using (var db = new NerdDinners())
{
var product = new Dinner { DinnerID = 1, Title = 101 };
db.Dinners.Add(product);
int recordsAffected = db.SaveChanges();
}

}
}
}

最佳答案

没有任何连接信息。 EF 将尝试在 SQL Express 而不是 SQL Server 中创建数据库。如果您希望在 SQL Server 中创建它,您将需要提供一个连接字符串。

关于c# - EF 代码优先 : Cannot connect to SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580431/

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