2017-11-15 10 views
-1

SQL Serverのインスタンスが2つあります - SQLEXPRESSMSSQLServer 2017です。 My Entity Frameworkテストアプリケーションは、データベースをSQLEXPRESSに作成します。 SQL Server Entity Frameworkのどのインスタンスを使用すべきかはどこで定義されていますか?既定のSQL Server for Entity Framework

テストアプリケーション:

public class Person 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 

    public class PeopleContext : DbContext 
    { 
     public IDbSet<Person> People { get; set; } 
    } 

    static void Main(string[] args) // using DB 
    { 
     try 
     { 
      using (PeopleContext ctx = new PeopleContext()) 
      { 
       ctx.People.Add(new Person() { Id = 1, Name = "John Doe" }); 
       ctx.SaveChanges(); 
      } 

      using (PeopleContext ctx = new PeopleContext()) 
      { 
       Person person = ctx.People.SingleOrDefault(p => p.Id == 1); 
       Console.WriteLine(person.Name); 
      } 

     } 
     catch (Exception e) 
     { 

      Console.WriteLine(e.Message); 
     } 

     Console.WriteLine("finish"); 
     Console.ReadLine(); 
    } 

UPD: 私は私のapp.configにいくつかの接続文字列を持っている:

<?xml version="1.0" encoding="utf-8"?> 
<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> 
    <connectionStrings> 
    <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    <add name="LearnCSharpConn_server" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=LearnCSharp;Integrated Security=True" /> 
    <add name="LearnCSharpConn" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LearnCSharp.mdf;Integrated Security=True" /> 
    </connectionStrings> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

EFを使用するかを決める方法は?

+0

私はいくつかの接続文字列を持っています。 EFはどちらを使用するかをどのように決定するのですか? UPDの詳細。 – vico

+0

@Vico:非具体的なDbContextでSQL Expressを選択した場合、SQL Expressがデフォルトとして選択されている可能性があります。 MSDN、 "接続文字列を指定しない場合、Entity Frameworkは、DbContextクラスの完全修飾名を使用してユーザーディレクトリにLocalDBデータベースを作成します。" https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-stringの終わりを参照してください。 –

+0

@HugoUchoBruno Nevermind、ユーザーがコメントを削除しました。 –

答えて

0

あなたは、この使用することができます:あなたが見つけるために求めるべきserverusernamepassworddefault databaseを検出するには

<connectionStrings> 
<add name="Database" connectionString="Data Source=source;Initial Catalog=Your_database;Integrated Security=True" providerName="System.Data.SqlClient" /> 

-1

public class PeopleContext : DbContext 
{ 
    public PeopleContext() : base("Database") //it makes reference to the connection string defined in the app.config 
    {  
    } 
    public IDbSet<Person> People { get; set; } 
} 

そして、あなたのapp.config内を接続文字列はエンティティフレームワークで使用されています。

ただし、ほとんどのバージョンはweb.configまたはapp.configのいずれかでの接続文字列を格納し、使用エンティティフレームワークのバージョンを指定しないでください。

代わりに、接続文字列を表示するには、Visual Studioのデバッグモードを経由してPeopleContextコンストラクタにステップインしたり、ネットコアに接続文字列がPeopleContext.csOnConfigure方法をステップ実行することにより求めることができます。

Ctrl + fに失敗すると、ソリューション全体でconnectionStringを検索できません。

関連する問題