4

http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspxのように、EFコードファーストを使用してデータベースを作成しました。しかし、データを db に入力してからadd()呼び出すsavechanges()と、SQL Server データベース フォルダーに新しいデータベースが表示されず、例外はありません。そうですか?データベースはどこにありますか? データベース フォルダに入れるにはどうすればよいですか?

私はこのコードで作業します:

     public class Name
      {
       public long NameId { get; set; }
       public string FullName { get; set; }
      }

   public class InfoContext : DbContext
     {
       public DbSet<Name> Names { get; set; }        
     }

それから私はそれを呼びます:

      var db = new InfoContext();
        var Names = new Name
        {
            NameId = 1,
            FullName = "test"
        };
        db.Names.Add(Name);

        db.SaveChanges();
        var test = db.Names.Find(1);//there I get correct value

次のように web.config に connectionString があります。

     <connectionStrings>
     <add name="InfoName" providerName="System.Data.SqlClient" 
     connectionString="Server = .\MYINSTANCE; Initial Catalog=mydbname;" />
   </connectionStrings>
4

3 に答える 3

2

コメントに基づいてweb.config、プロジェクト ルートのファイルを変更する必要があります (Viewsフォルダー内のファイルではありません。次のようにセクションを追加できます。

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings> 

要素の name プロパティは、Data モデルの DbContext 名であるため、クラスが次のように定義されている場合:

public class SomeContext : DbContext
{
      ...
}

次に、構成は次のようになります。

<connectionStrings>
    <add name="SomeContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>

接続文字列に関しては、データベースに依存します。

于 2013-08-17T15:26:02.857 に答える