我想要在Asp.Net核心文檔中添加一個View組件。該組件使用內存數據存儲。但我無法讓內存工作。我不明白如何遵守錯誤消息中的說明。我錯過了什麼? 這是錯誤:內存中的Asp.Net核心實體框架 - 沒有提供商
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
在此代碼發生在線context.add(新的TodoItem):
namespace ViewComponentSample.Models
{
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
var context = serviceProvider.GetService<ToDoContext>();
if (context.Database == null)
{
throw new Exception("DB is null");
}
for (int i = 0; i < 9; i++)
{
context.Add(new TodoItem()
{
IsDone = i % 3 == 0,
Name = "Task " + (i + 1),
Priority = i % 5 + 1
});
}
context.SaveChanges();
}
}
}
我在project.json這些依賴關係:
"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.InMemory": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"BundlerMinifier.Core": "2.2.281",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.NETCore.App": "1.0.0",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0",
"Microsoft.Extensions.DependencyInjection": "1.0.0",
"Microsoft.AspNet.Routing": "1.0.0-rc1-final"
},
這是我Startup.cs ConfigureServices:
services.AddEntityFrameworkInMemoryDatabase().AddDbContext<ToDoContext>(optionsAction => optionsAction.UseInMemoryDatabase());
這是型號:
using Microsoft.EntityFrameworkCore;
namespace ViewComponentSample.Models
{
public class ToDoContext : DbContext
{
public DbSet<TodoItem> ToDo { get; set; }
}
}
謝謝,但我得到了同樣的錯誤。服務不需要知道這是Entity Framework嗎? –
1)直接調用'context.TodoItems.Add(...)'會發生什麼? 2)你能告訴我你的ToDoContext嗎? – rudolfdobias
3)您的DbContext構造函數是否接受錯誤中聲明的'DbContextOptions'參數? – rudolfdobias