0

hier zu injizieren ist mein asp.net Kernprojektstrukturasp.net Kern web api, wie die Verbindungszeichenfolge

1- ASP.NET CORE Web API (enthält aspsettings.json)

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

    } 

2-Services-Projekt (Web-API-Aufruf der Methode von Dienstleistungen Projekt)
3-Endlagerprojekt (Services aufrufen Methode von Repository-Projekt und Repository-Projekt gehört das DATA Projekt wHE wieder sind alle Modelle)

4-DATA-Projekt, wo es mit dem Code die ganze Modell ist enthalten erste

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) 
     { 
.... 

Wie Sie sehen können, habe ich die Verbindung auf der Methode OnConfiguring fest einprogrammiert, die nicht das ist Best Practice sicher.

Gibt es eine Möglichkeit, die Verbindungszeichenfolge aus der Konfigurationsdatei des Web-API-Projekts zu übergeben?

Funktioniert der update-Datenbankbefehl noch, wenn wir die Verbindung aus der Datei aspsettings.json aus dem Web-API-Projekt bestehen?

Vielen Dank

+1

Können Sie Code zum Erstellen von TtEntities-Instanz in ASP.NET CORE Web API bereitstellen? Haben Sie DI dafür verwendet? – itikhomi

Antwort

1

DI löst dieses Problem perfekt und .NET Core 2.0 hat Microsoft DI das ist mit DI deutlich Erfahrung bietet.

oh, lässt beginnt (ich denke, dass DATA Projekt und Endlagerprojekt sollte eine sein)

public class REPOSITORYClass 
    { 
     private readonly TtEntities _db; 

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

     //some your staff of REPOSITORYClass thats uses _db 
    } 

jetzt SERVICES Projekt gehen

von Endlagerprojekt

auf Ihre REPOSITORYClass ändern

lässt Ändern Sie einen Dienst, der REPOSITORYClass verwendet

public class SomeService 
    { 
     private readonly REPOSITORYClass _repo; 

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

     //other staff of SomeService thats uses _repo 
    } 

Danach gehen Sie zu ASP.NET CORE Web-API-Startup-Datei und fügen Sie

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>(); 

gehen nun auf die contoller das ist verwendet Ihre Someservice

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(); 
      } 
     } 

und nutzen alle Ihre Wünsche von Ihrem Service, der mit Depos von Repo injiziert und DB-Entität mit Verbindung

injiziert wie

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"))); 
    } 
} 

Überlegen Sie, wie DefaultConnection 13. Auch appsettings eine Probe in Zeile verwendet wird, ist wie folgt:

das ist alles

PS auch diese Introduction to Dependency Injection in ASP.NET Core

0

Eine einfache Lösung wie diese zu lesen empfehlen :

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication5;" 
    } 
}