0

は、ここに私のasp.netコアプロジェクト構造asp.netコアウェブAPIは、接続文字列を注入する方法を

1 - ASP.NETのCOREのWeb API(含まれていaspsettings.json)

"ConnectionStrings": { 
    "DefaultConnection": "Server=(local)\\SQLEXPRESS;Database=testdb;Trusted_Connection=True;" 

    } 
です

2-SERVICESプロジェクト(サービスプロジェクトからのWeb API呼び出し方法)
3リポジトリプロジェクト(サービスリポジトリプロジェクトとリポジトリプロジェクトからメソッドを呼び出すには、WHEのDATAプロジェクトを含めます

public class TtEntities : DbContext 
    { 

     public virtual DbSet<RoomMessage> RoomMessage { get; set; } 
     public virtual DbSet<UserRoom> UserRoom { get; set; } 


     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
     { 
      optionsBuilder.UseSqlServer(@"Server=(local)\SQLEXPRESS;Database=testdb;Trusted_Connection=True;"); 
     } 

     protected override void OnModelCreating(ModelBuilder modelBuilder) 
     { 
.... 

)されているすべてのモデルの再それはコードで、すべてのモデルが含まれています

4-DATAプロジェクトが最初にあなたが見ることができるように、私はない方法OnConfiguringの接続をハードコード確かにベストプラクティス。

Web APIプロジェクトの構成ファイルから接続文字列を渡す方法はありますか?

Webアプリケーションプロジェクトからaspsettings.jsonファイルからの接続を渡すと、update databaseコマンドは機能しますか?

どうもありがとう

+1

ASP.NET CORE Web APIでTtEntitiesインスタンスを作成するコードを提供できますか?これにDIを使用しましたか? – itikhomi

答えて

1

DIは完全にこの問題を解決し、.NETのコア2.0は、Microsoft DIはthatsのは明らかにDIの経験を提供しています。

ああ、今SERVICESプロジェクト

することができますに行くリポジトリプロジェクトから開始する(私は、DATAプロジェクトとリポジトリ・プロジェクトは1でなければならないことだと思う)

public class REPOSITORYClass 
    { 
     private readonly TtEntities _db; 

     public REPOSITORYClass (TtEntities db){ 
      _db = db; 
     } 

     //some your staff of REPOSITORYClass thats uses _db 
    } 

にごREPOSITORYClassを変更 することができますREPOSITORYClassを使用する一部のサービスを変更する

public class SomeService 
    { 
     private readonly REPOSITORYClass _repo; 

     public SomeService (REPOSITORYClass repo){ 
      _repo = repo; 
     } 

     //other staff of SomeService thats uses _repo 
    } 
その後

がASPに移動します。

NET COREウェブAPI起動ファイルと

に追加します。public void ConfigureServices

 // Get connection of your repo 
     string connection = Configuration.GetConnectionString("DefaultConnection"); 
     // add TtEntities as service 
     services.AddDbContext<TtEntities>(options => 
         options.UseSqlServer(connection)); 

//add your repo 
     services.AddTransient<REPOSITORYClass>(); 
//add your service 
     services.AddTransient<SomeService>(); 

今contollerのザッツに行く

public class SomeController: Controller 
     { 
      private readonly SomeService _someService; 

      public SomeController(SomeService someService){ 
       _someService = someService; 
      } 

      //And use whatever your wants from your service that injected with deps of repo and injected db entity with connection 
      public string SomeMethod() 
      { 
       return _someService.SomeMethod(); 
      } 
     } 

と使用あなたからのどんなご要望ごSomeServiceを使用していますレポのdepsを注入し、接続したdbエンティティを注入したサービス

また、サンプル appsettingsが続くように似ている

public class Startup 
{ 
    public Startup(IConfiguration configuration) 
    { 
     Configuration = configuration; 
    } 

    public IConfiguration Configuration { get; } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 
    } 
} 

はライン13で使用される方法DefaultConnection考えてみましょう:

のthatsはすべて

PSも簡単な解決策は、このようなものです。このIntroduction to Dependency Injection in ASP.NET Core

0

読むことをお勧めします:

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication5;" 
    } 
} 
関連する問題