2017-02-12 130 views
0

6天前我遇到了一個問題,我仍然無法弄清楚。ASP.NET核心數據庫配置與依賴注入問題

我建立的DbContext使用DI:

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddDbContext<ApplicationContext>(options => 
     options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); 

    services.AddMvc(); 

    // Add application services. 
    services.AddTransient<IEmailSender, AuthMessageSender>(); 
    services.AddTransient<ISmsSender, AuthMessageSender>(); 
} 

數據庫文件路徑在appsettings.json

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Data Source=xxxx.db" 
    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    } 
} 

configurered而我創建的DbContext像這樣(https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

public class ApplicationContext : DbContext 
{ 
     public DbSet<Network> xxxx { get; set; } 
     public DbSet<NetworkEntry> xxxx { get; set; } 
     public ApplicationContext(DbContextOptions<ApplicationContext> options) 
     : base(options) 
     { } 
} 

我的控制器代碼:

public JsonResult Index(ApplicationContext db) 
{ 
    return Json(db.Networks); 
} 

它編譯並運行。如果我使用配置DB的另一種方式,它的工作原理100%問題免費:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 
    optionsBuilder.UseSqlite("Filename=./blog.db"); 
} 

我遇到的問題:

System.InvalidOperationException: Could not create a model binder for model object of type 'xxx.Data.ApplicationContext'. 
    at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinder(ModelBinderFactoryContext context) 
    at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindModelAsync>d__8.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.DefaultControllerArgumentBinder.<BindArgumentsCoreAsync>d__6.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.<Invoke>d__5.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() 

任何線索?

+0

您是否將此ApplicationContext注入某個MVC模型?你應該保持你的MVC模型清除任何依賴。一個MVC模型==數據。 – Steven

+0

不,根據我的理解,我的ApplicationContext沒有注入任何MVC模型。我的模型只有1個依賴項,它們是'System.ComponentModel.DataAnnotations;'它們使用字符串作爲數據,而使用int作爲鍵(ID) – Elitenudel

+0

你有沒有解決過這個問題?我也一樣 –

回答

0

我剛剛解決了類似的問題。對我來說問題是我誤解了依賴注入。上下文不會傳遞給Controller方法,而是傳遞給Controller的構造函數。

public class NetworksController : Controller 
{ 
    private readonly ChoirAdminContext db; 

    public NetworksController(ApplicationContext context) 
    { 
     db = context; 
    } 

    public JsonResult Index() 
    { 
     return Json(db.Networks); 
    } 
}