0

新しいモデルを作成しました:

 public class AuthorDatacontext : DbContext
    {
        public DbSet<Author> Authors { get; set; }

        static AuthorDatacontext()
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AuthorDatacontext>());
        }
    }

接続文字列:

  <add name="MyLibrary.Models.AuthorDatacontext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MyLibrary.Models.AuthorDatacontext.mdf" providerName = "System.Data.SqlClient"  />

そして私はそれを私のコントローラーで使用しました:

var db = new AuthorDatacontext();
                db.Authors.Add(author);
                db.SaveChanges();

私は以前、別のプロジェクトでそれを行いましたが、うまくいきました。しかし、新しい作成者を保存しようとすると、エラーが表示されます。

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

私は何を間違っていますか?接続を削除してデフォルトの接続のみを残すと、すべて正常に動作します。

前のプロジェクトには同じ接続文字列がありました。

<add name="MvcAuction.Models.AuctionsDataContext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MvcAuction.Models.AuctionsDataContext.mdf" providerName = "System.Data.SqlClient"  />
4

1 に答える 1

1

問題は、EF がプロジェクトから MDF を再度作成しようとすることだと思います。まずは削除してみてください。

\MvcAuction.Models.AuctionsDataContext.mdfEF は、これがその場所に存在するかどうかを確認しようとします。SQL サーバーを使用していた場合、変更は CODE First からデータベースに移行されます。

于 2013-05-08T09:03:38.183 に答える