2016-03-23 7 views
0

エンティティフレームワークの新機能です。私は、コードファーストアプローチを使用したシンプルなプロジェクトを持っています。エンティティフレームワークコードファースト接続サーバーエラー

これはコードです:

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

接続文字列自動エンティティフレームワークによって作成:

データソース=(localdb)\ mssqllocaldb;初期カタログ= EFExample.Model.SchoolContext;統合セキュリティ= True; MultipleActiveResultSets = True

私はsqlserver 2012 expressをインストールしました。 、2つのインスタンスで:

*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

NTサービス\ MSSQL $ SQLEXPRESS私はプログラムを実行した

NTサービス\ MSSQLSERVER、私はエラーを取得します

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> 

間違っているものを私を助けてください? ありがとうございます。

+0

で独自れたconnectionStringを追加し、明示的にお勧めしたいあなたは 'app.config'の' <のconnectionStrings> 'セクションを投稿することができますか? –

+0

'(localdb)'は通常、SQLベースのSQLデータベース(SqLiteなど)であるSQL Server LocalDBを指します。それを 'Data Source = localhost \ mssqllocaldb'に変更してみてください。 – CodingGorilla

+0

@ Chris Pickford:私はapp.configでconnectionstringを使用しません。私はEFが自動的に接続文字列を作成すると思います。 – nguyenmy

答えて

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>` 
+0

ありがとうございます。それはうまくいく。 – nguyenmy

関連する問題