11

ASP .Net MVC Web アプリで Hangfire を使用していますが、正常にインストールされました。データを保存するために使用したのと同じ LocalDb を使用して、Hangfire がデキューして処理するキューに入れられたジョブを保存したいと考えています。ただし、で定義された接続文字列または名前を指定すると、以下のエラーが発生しWeb.configますStartp.cs。ハングファイアする前に、同じ localDb の更新データを追加、削除するのに問題はありませんでした。

Cannot attach the file 'c:\users\jerry_dev\documents\visual studio 2013\Projects\Hangfire.Highlighter\Hangfire.Highlighter\App_Data\aspnet-Hangfire.Highlighter-20150113085546.mdf' as database 'aspnet-Hangfire.Highlighter-20150113085546'.

Startup.cs:

public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            app.UseHangfire(config =>
            {
                string hangfireConnectionString = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True";
                config.UseSqlServerStorage(hangfireConnectionString);
                config.UseServer();
            });
        }

私のプロジェクトのソリューションは「Hangfire.Highlighter」という名前です

Web.config:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Hangfire.Highlighter-20150113085546.mdf;Initial Catalog=aspnet-Hangfire.Highlighter-20150113085546;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
4

5 に答える 5

21

私はこれが古いことを知っています-しかし、9か月が経過し、これについても髪を引っ張りました-そして、ここに書き留めることにしました

私の解決策は、簡単で汚れた DbContext を作成し、それを適切な接続文字列にポイントし、コンストラクターで Database.CreateIfNotExists を呼び出すことでした。

public class HangfireContext : DbContext
{
    public HangfireContext() : base("name=HangfireContext")
    {
        Database.SetInitializer<HangfireContext>(null);
        Database.CreateIfNotExists();
    }
}

HangfireBootstrapper.Start() メソッドでは、次のようにします。

public void Start()
{
    lock (_lockObject)
    {
        if (_started) return;
        _started = true;

        HostingEnvironment.RegisterObject(this);

        //This will create the DB if it doesn't exist
        var db = new HangfireContext();

        GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireContext");

       // See the next section on why we set the ServerName
        var options = new BackgroundJobServerOptions()
        {
            ServerName = ConfigurationManager.AppSettings["HangfireServerName"]
        };

        _backgroundJobServer = new BackgroundJobServer(options);

        var jobStarter = DependencyResolver.Current.GetService<JobBootstrapper>();

        //See the Recurring Jobs + SimpleInjector section
        jobStarter.Bootstrap();

    }
}

Hangfire が LocalDb でこれほど苦労している理由がわかりません。本格的な SQL インスタンスしか処理できないのではないでしょうか? いずれにせよ、これは私、新しいチームメンバー、および立ち上がる新しい開発/ステージング/製品インスタンスにとって機能します。

于 2015-08-30T02:20:54.193 に答える
4

私もこれが古いことを知っていますが、最近これに遭遇しました。これが私の修正です:

  1. Visual Studio で、[表示] -> [SQL Server オブジェクト エクスプローラー] に移動します。
  2. まだ接続されていない場合は、データ ソースに接続します。上記の例では、「(LocalDb)\v11.0」です。
  3. 「データベース」を右クリック -> 「新しいデータベースを追加」
  4. データベース名を入力 = 例: 'aspnet-Hangfire.Highlighter-20150113085546' または接続文字列でデータベースに付けた名前。
  5. Fill Database location = これは、アプリケーションのデータ ディレクトリ、MVC プロジェクトの「App_Data」である必要があります。

これで私の場合の問題は解決しました。

于 2015-12-21T20:42:35.923 に答える
0

DBはすでに作成されていますか?「Server=.;Database=HangFire.Highlighter;Trusted_Connection=True;」のような別の接続文字列形式を使用してみてください。

于 2015-01-16T14:56:38.273 に答える