EF code首先:无法连接到SQL Server [英] EF Code First: Cannot connect to SQL Server

查看:118
本文介绍了EF code首先:无法连接到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建使用code-第一种方法的数据库。当我运行下面的code我收到以下异常。我们怎样才能克服这个?

注:

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

异常

  

提供程序未返回ProviderManifestToken字符串

信息

  

此操作需要的主数据库的连接。无法   创建到主数据库,因为原来的连接   数据库连接已经打开和凭证已被删除   从连接字符串。供应未打开的连接。

内部异常

  

时出现与网络相关的或特定于实例的错误,而   建立SQL Server的连接。服务器未找到或   无法访问。验证实例名称是否正确,以及   SQL Server配置为允许远程连接。 (provider:命名   管道提供商,错误:40 - 无法打开到SQL连接   服务器)

code:

 使用System.Data.Entity的;
命名空间LijosEF
{
  公共类晚餐
  {
      公众诠释DinnerID {获得;组; }
      公众诠释标题{获得;组; }
  }

  公共类RSVP
  {
    公众诠释RSVPID {获得;组; }
    公众诠释DinnerID {获得;组; }

    公共虚拟晚宴晚宴{获得;组; }
  }

  //System.Data.Entity.DbContext是EntityFramework.dll
  公共类NerdDinners:System.Data.Entity.DbContext
  {
    公共DbSet<晚餐>晚餐{获得;组; }
    公共DbSet< RSVP>回函{获得;组; }
  }
}

命名空间LijosEF
{
类节目
{
    静态无效的主要(字串[] args)
    {
        使用(VAR DB =新NerdDinners())
        {
            变种产品=新的晚宴{DinnerID = 1,标题= 101};
            db.Dinners.Add(产品);
            INT recordsAffected = db.SaveChanges();
        }

    }
}
}
 

解决方案

在没有任何连接信息。 EF将尝试创建数据库的SQL防爆preSS和不是SQL Server。如果你想在SQL Server中创建你需要提供一个连接字符串。

I am trying to create a database using code-first approach. When I am running the following code I am getting the following exception. How can we overcome this?

Notes:

  • I have SQL Server 2008 R2 on my system.
  • I am not using any config file. I assumed that it will create database using conventions
  • There is no open connection already; I just restarted the system and tested. Still the same issue.
  • Runtime version for EntityFramework.dll is v4.0.3xxx

Exception:

The provider did not return a ProviderManifestToken string

Message :

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.

Inner Exception:

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)

Code:

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();
        }

    }
}
}

解决方案

Without any connection information. EF will try to create the database in SQL Express and not SQL Server. If you want it created in SQL Server you will need to provide a connection string.

这篇关于EF code首先:无法连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆