4

ADO.NETチームのCodeFirstMigrationsからの投稿をフォローしています:Alpha3'No-Magic'ウォークスルー

asp.netmvcWebアプリケーションで移行するためにSQLを使用してファイルを読み取ろうとしています。私はSQLメソッドを使用しています。問題は、何をしてもプロジェクトへのルートパスを取得できないことです。私は常に'c:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \IDE\'を取得します

誰かが私のファイルを開く方法を教えてもらえますか?

ありがとう。

編集済み

public partial class AddMembership : DbMigration
{
    public override void Up()
    {
        //var file = File.OpenText(System.IO.Path.GetDirectoryName() + "Model/Schema/membership-up.sql");
        //var file = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Model/Schema/membership-up.sql");
        //var path = HttpContext.Current.Request.MapPath("/Models/Schema/membership-up.sql");
        var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
        Sql(file.ReadToEnd());
    }

    public override void Down()
    {
        // ....
    }
}
4

2 に答える 2

0

Server.MapPathの使用についてはどうですか

http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

于 2011-10-30T19:25:47.343 に答える
0

サイトから直接移行を実行するには、このコードを追加できます

var dbMigrator = new DbMigrator(new Settings());
dbMigrator.Update();

いずれかのプロセス (IIS とパッケージ マネージャー コンソール) から移行を実行するには、Server.MapPath を使用する前にサーバー オブジェクトが作成されているかどうかを最初に確認できます。Server オブジェクトが null の場合、パッケージ マネージャー コンソールの下にいるため、より適切なものを使用して現在のパスを取得できます。

Directory.GetCurrentDirectory();

したがって、このコードを置き換えます

var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");

このようなもので

String path;
if (HttpContext.Current.Server != null) {
    path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
} else {
    path = Path.Combine(Directory.GetCurrentDirectory(), "Models/Schema/membership-up.sql");
}
于 2011-11-12T23:21:36.607 に答える