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=\"Web\" /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>
あなたの質問は何ですか?あなたのプログラムは実際に走っていますか? – CodeCaster
はい、接続文字列要素なしで実行されています。私はSQLEpressのデータベースを見つけましたが、接続情報をどのように決定したのですか? – Mohamed