エンティティフレームワークの新機能です。私は、コードファーストアプローチを使用したシンプルなプロジェクトを持っています。エンティティフレームワークコードファースト接続サーバーエラー
これはコードです:
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*
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>
間違っているものを私を助けてください? ありがとうございます。
で独自れたconnectionStringを追加し、明示的にお勧めしたいあなたは 'app.config'の' <のconnectionStrings> 'セクションを投稿することができますか? –
'(localdb)'は通常、SQLベースのSQLデータベース(SqLiteなど)であるSQL Server LocalDBを指します。それを 'Data Source = localhost \ mssqllocaldb'に変更してみてください。 – CodingGorilla
@ Chris Pickford:私はapp.configでconnectionstringを使用しません。私はEFが自動的に接続文字列を作成すると思います。 – nguyenmy