2017-01-22 13 views
1

次のように私の溶液中の二つのプロジェクトがあります:EFコア1.1 - .NETコアコンソールで移行を追加する方法

  • Sample.UI作成者:DOTNETコアコンソールプロジェクト「dotnetに新しいです」 。
  • Sample.Infrastruction: "dotnet new -t lib"で作成されたdotnetコアライブラリで、BlogDbContextがそこにあります。

    No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.On Configuring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext

    エラー・スタック情報を:

    at Microsoft.EntityFrameworkCore.Internal.DatabaseProviderSelector.SelectServices() at Microsoft.EntityFrameworkCore.Internal.LazyRef1.get_Value()

    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Vis itScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)

    at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.b__0(ServiceProvider provider)

    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

    at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)

    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)

    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)

    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()

    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

    私が試してみました私はSample.Infrastructureフォルダにdotnet ef --startup-project ..\sample.ui migrations add InitialDBを実行しようとすると、それはエラーが発生した

問題を解決するためにAddDbContextを追加しますが、私のためには機能しませんでした。たぶん私は内蔵のDIを正しく使用していないでしょう。私はたくさんのGoogle検索を行っていますが、ほとんどのソリューションは、ドットネットコンソールではなくasp.netコアに使用されています。

namespace Sample.Infrastructure 
{ 
    public class BlogDbContext : DbContext 
    { 
     public BlogDbContext(DbContextOptions<BlogDbContext> options) 
      : base(options) 
     { 

     } 

     public BlogDbContext() 
     { 
      //ATT: I also don't understand why ef requires the parameterless constructor 
     } 

     public DbSet<Blog> Blogs { get; set; } 

     protected override void OnModelCreating(ModelBuilder builder) 
     { 
      builder.Entity<Blog>().HasKey(post => post.Identity); 
      base.OnModelCreating(builder); 
     } 
    } 
} 

Sample.UIソースコード:あなたの参考のため


は、以下Sample.Infrastructureソースコードである

namespace Sample.UI 
{ 
    public class Program 
    { 
     static public IConfigurationRoot Configuration { get; set; } 
     private static IServiceProvider _serviceProvider; 

     public static void Main(string[] args) 
     { 
      var builder = new ConfigurationBuilder() 
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); 
      Configuration = builder.Build(); 

      //setup DI 
      _serviceProvider = new ServiceCollection() 
       .AddDbContext<BlogDbContext>(o=> o.UseSqlite(Configuration["ConnectionString"])) 
       .BuildServiceProvider(); 

      // PostApplicationService app = new PostApplicationService(); 
      // var ctx = _serviceProvider.GetService(typeof(BlogDbContext)) as BlogDbContext; 
      // app.Inital(ctx); 

      Console.ReadLine(); 
     } 
    } 
} 

更新

また、私は@ Mortenのソリューションを試してみました.Sample.UIフォルダで次のコマンドを実行してください。しかし、私はまったく同じエラーを受けました。

dotnet ef --project "C:\SampleDDD\Sample.Infrastructure" --startup-project "C:\SampleDDD\Sample.UI" migrations add "InitalDB" 

答えて

3

にれたconnectionStringを設定しようと同じSample.UIプロジェクトから

それを実行します。このようなEFコアの移行時に、ほとんどのコモンズ間違いを避けるために、fo

  1. llowing

    は、明示的にコマンド
    --startup-プロジェクト< startprojectの>、サンプル中のプロジェクトのパラメータを記述します。あなたのケースではUI
    --project < ContextProject>、これはデフォルトでabsent.Soある場合は、デフォルトを使用する場合は、あなたのケースで

  2. 移行はpublic default constructorまたはIDbContextFactoryと連携、Sample.InfrastructureあなたDBContext葉のプロジェクトですあなたは、あなたのコンテキストクラスにあなたがIDbContextFactoryを実装することができ

public class BlogDbContext: DbContext 
{ 
    public DbSet<Blog> Blogs { get; set; } 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
    string connStr = ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString; 
    optionsBuilder.UseSqlite(connStr); 
    } 
} 

alternativlyのようなものをOnConfiguringを上書きする必要がありますコンストラクタあなたのBlogDbContextが存在する同じプロジェクトで

public class YourFactory : IDbContextFactory<BlogDbContext> 
{ 
    public BlogDbContextCreate() 
    { 
     var optionsBuilder = new DbContextOptionsBuilder<BlogDbContext>(); 
     string connStr = ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString; 
     optionsBuilder.UseSqlite(connStr); 

     return new BlogDbContext(optionsBuilder.Options); 
    } 
} 
+0

ありがとう@nsb、私はファクトリのソリューションが好きです。私のBlogDbContextは汚染されません。 – Charlie

+1

optionsBuilder.UseSqlite(Configuration ["ConnectionString"]);もう動作しません。更新してください –

0

Sample.Infrascruture--projectと追加する必要があります。

dotnet ef --project "C:\MYPATH\Sample.Infrastructure" --startup-project "C:\MYPATH\Sample.UI" migrations add "TXT" 

そして、あなたはまたdatabase upgrade

を行う際に、BlogDbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 
    optionsBuilder.UseSqlServer("Server=.;Database=MYDB;Integrated Security=True"); 
} 
+0

私はあなたのやり方を試しましたが、同じエラーが発生しました。あなたはそうすることに成功しましたか? – Charlie

+0

@Charleyはい、これが私のやり方です。私はこの問題について数時間を使いました。 –

+0

いくつかのエラースタック情報を含めるように質問を更新しました。およびあなたのソリューションに関するフィードバック。あなたは私がそれを見るのを手助けすることができますか、他の情報が必要な場合は教えてください – Charlie

関連する問題