2016-11-21 7 views
0

EFでMVCプロジェクトを作成しましたが、最初に作成したときにDBContextに接続文字列を定義しませんでした。なぜなら、デフォルトを使用したかったからですが、web.configファイルを開き、connectionStrings素子! は、私が使用しているデータベースとそのSQLサーバーの名前を見てみたい...web.configのconnectionStringsが見つかりません?

編集

ここ

である私のモデルクラス:?

namespace MVCAppForTests.Models 
{ 
    public class BookContext : DbContext 
    { 
     public DbSet<Book> Books { get; set; } 
    } 
    public enum Rating 
    { 
     Excellent, 
     VeryGood, 
     Good, 
     OK, 
     Bad 
    } 
    public class Book 
    { 
     public int ID { get; set; } 
     public string Title { get; set; } 
     [Required] 
     public string Author { get; set; } = "Anonymous"; 
     public Rating Rating { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime DateCreated { get; set; } = DateTime.Now; 
    } 
    public class Initializer : DropCreateDatabaseIfModelChanges<BookContext> 
    { 
     protected override void Seed(BookContext context) 
     { 
      var _books = new List<Book> 
     { 
      new Book { ID=1, Title="Awful Streets", Author="Tom Raouf", Rating= Rating.Good}, 
      new Book { ID=2, Title="Green Lights", Author="Sam Bruce", Rating= Rating.Bad}, 
      new Book { ID=3, Title="Bloodish breaths", Author="Yara Tarek", Rating= Rating.VeryGood}, 
      new Book { ID=4, Title="Happy inside", Author="Norman Zory", Rating= Rating.Bad}, 
      new Book { ID=5, Title="I lived again", Author="Adrian T. William", Rating= Rating.Excellent}, 
      new Book { ID=6, Title="And she knocked a door", Author="Tom Raouf", Rating= Rating.OK} 
     }; 
      _books.ForEach(c => context.Books.Add(c)); 
      context.SaveChanges(); 
     } 
    } 
} 

web.configファイル:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<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> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 
    </system.web> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    </system.webServer> 
    <entityFramework> 
    <contexts> 
     <context type="MVCAppForTests.Models.BookContext, MVCAppForTests" > 
     <databaseInitializer type="MVCAppForTests.Models.Initializer, MVCAppForTests"></databaseInitializer> 
     </context> 
    </contexts> 
    <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> 
+0

あなたの質問は何ですか?あなたのプログラムは実際に走っていますか? – CodeCaster

+0

はい、接続文字列要素なしで実行されています。私はSQLEpressのデータベースを見つけましたが、接続情報をどのように決定したのですか? – Mohamed

答えて

0

エンティティフレームワークはすべて約規約です。接続文字列(名前)を指定しないと、慣例によって1つが決まります。

この

MSDN: Entity Framework Connections and Modelsで説明されています。

は、あなたがそれまでに作成したデータベース接続でコードファーストモードで実行するようにDbContextの原因となりますDbContextのパラメータなしのコンストラクタを呼び出して、アプリケーション内の他のコンフィギュレーションを行っていない場合はコンベンション。

[...] DbContextは、あなたの派生コンテキストクラスDemo.EF.BloggingContext-としてデータベース名の名前空間修飾名を使用し、SQL Expressのいずれかを使用して、このデータベースの接続文字列を作成します。この例では

またはLocalDb。どちらもインストールされている場合は、SQL Expressが使用されます。

+0

DBContextコンストラクタを呼び出さずに多くのプロジェクトを作成しましたが、web.configで定義しています。 – Mohamed

+0

コメント。 – CodeCaster

+0

多くのデモでは定義がわからないので、このようにコンテキストクラスを作成するのは初めてです(基本クラスのコンストラクタで/ nameを渡さない/パラメータのないコンストラクタを使用する)。どのようにconnectonの文字列が私のために作成されているか – Mohamed

関連する問題