私はASP.NET CoreとEntity Framework Coreアプリケーションを作成しています。データアクセスレイヤーを別のアセンブリに格納したいので、このチュートリアルに従いました: http : //www.michael-whelan.net/ef-core-101-独立したアセンブリでの移行/
しかし、私はまた、接続文字列のハードコーディングを避けたいと思います。私はそれをJSON設定ファイルまたは環境変数として保存しようとしましたが、 ConfigurationBuilder
を使用して取得しようとしましたが、コマンドライン移行ツールdotnet ef migrations
を使用しているときは利用できません。この問題を解決する方法はありますか? .NET CoreとEF Coreの両方の1.0.1バージョンを使用しています。
この問題を解決するために、私はDbContextから派生したDbContextを使用して移行のためのクラスライブラリを作成しますが、接続された接続文字列はハードに接続されています。
using Microsoft.EntityFrameworkCore;
namespace ChatLe.Repository.Identity.SqlServer
{
public class ChatLeIdentityDbContext: ChatLe.Models.ChatLeIdentityDbContext
{
public ChatLeIdentityDbContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=chatle;Trusted_Connection=True;MultipleActiveResultSets=true");
base.OnConfiguring(optionsBuilder);
}
}
}
次に、私はef toolコマンドを次のように起動します:
dotnet ef --startup-project {path to my startup project} migrations add login-activity --context ChatLe.Repository.Identity.SqlServer.ChatLeIdentityDbContext
dotnet ef --startup-project {path to my startup project} database update --context ChatLe.Repository.Identity.SqlServer.ChatLeIdentityDbContext
私のgit hubプロジェクトの全サンプルを読む: https : //github.com/aguacongas/chatle/tree/develop/src/ChatLe.Repository.Identity.SqlServer