实体框架代码优先连接服务器错误

时间:2016-03-23 15:18:12

标签: c# sql-server entity-framework

我是Entity框架的新手。我有一个使用代码优先方法的简单项目。

这是代码:

 public class Student
    {
        public Student()
        {

        }

        [Key]
        public int id { get; set; }
        public string StudentName { get; set; }
    }



    public class SchoolContext:DbContext
    {
        public SchoolContext() : base()
        {
            Console.WriteLine(Database.Connection.ConnectionString);

        }

        public DbSet<Student> Students { get; set; }
    }

 static void Main(string[] args)
        {
            Student aa = new Student();
            aa.id=1 ;
            aa.StudentName = "eeee";
            using (var scContext = new SchoolContext())
            {
                scContext.Students.Add(aa);
                scContext.SaveChanges();
            }

            Console.Write("success");
        }

连接字符串由Entity framework自动创建:

  

数据源=(localdb)\ mssqllocaldb;初始目录= EFExample.Model.SchoolContext; Integrated Security = True; MultipleActiveResultSets = True

我安装了sqlserver 2012 express,有2个实例:

  

NT Service \ MSSQLSERVER

     

NT Service \ MSSQL $ SQLEXPRESS

当我运行该程序时,我收到一个错误:

*An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll

Additional information: 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* 

enter image description here

的App.config

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

请帮帮我怎么了? 提前谢谢。

3 个答案:

答案 0 :(得分:1)

我建议在app.config

中明确添加自己的连接字符串
`<connectionStrings>
    <add connectionString="Data Source=.\SQL_INSTANCE_NAME;Initial Catalog=DBName;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=False" name="DbConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>`

答案 1 :(得分:0)

我遇到了类似的麻烦,似乎第一次向sql server 2016提出了一个安装问题,而不是web.config文件中缺少一些连接字符串。 对于asp.net mvc中的代码,首先是一行:

public OdeToFoodDb():base(“name = DefaultConnection”)         {

答案 2 :(得分:0)

我遇到了类似的麻烦,似乎第一次向sql server 2016提出了一个安装问题,而不是web.config文件中缺少一些连接字符串。 对于asp.net mvc中的代码,首先是一行:

e.g。我的Model类是从DbContext派生的BeispielDb 此后'ctor'必须使用db连接字符串调用base'ctor':

public BeispielDb() : base("name=DefaultConnection")

构造函数已经使用web.config中的DefaultConnection字符串提供了DbContext(基类)构造函数。

度过美好的一天并保持良好状态,

Salomon MOYAL-BLOCH