Asp.net 5 Entity Framework的7.0.0-RC1决赛明确的交易错误 [英] Asp.net 5 Entity Framework 7.0.0-rc1-final explicit transaction error
问题描述
我想在ASP.NET项目中使用显式事务。调试时,只要与使用(VAR交易行= ...到达
以下异常被抛出:
I am trying to use an explicit transaction in an ASP.NET project. When debugging, as soon as the line with the using( var transaction = ... is reached the following exception is thrown:
没有服务类型Microsoft.Data.Entity.Storage.IRelationalConnection已注册
"No service for type 'Microsoft.Data.Entity.Storage.IRelationalConnection' has been registered"
我已经包含下面我的依赖关系。我失去了我的启动文件的东西吗?
I have included my dependencies below. Am I missing something in my Startup file?
using (ApplicationDbContext context = new ApplicationDbContext())
{
try
{
using (var transaction = context.Database.BeginTransaction())
{
//do some work
}
}
catch (Exception exc) { }
}
我的依赖关系:
"dependencies": {
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final" }
感谢
推荐答案
由于ApplicationDbContext尚未配置为使用特定的提供者,您收到此错误。有几种方法来配置你的DbContext指向你的数据库,如在配置EF Core的官方文档中创建的DbContext 。
You are getting this error because ApplicationDbContext has not been configured to use a specific provider. There are several ways to configure your DbContext to point to your database, as explained in Configuring a DbContext on EF Core's official docs.
例如,
public class ApplicationDbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("Filename=./test.db");
}
}
或者,如果你想使用依赖注入,
Or if you want to use dependency injection,
// in Startup.cs
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite("Filename=./test.db"));
// in a constructor argument
public HomeController(ApplicationDbContext context)
...
// in code
using(var context = serviceProvider.GetRequiredService<ApplicationDbContext>())
....
这篇关于Asp.net 5 Entity Framework的7.0.0-RC1决赛明确的交易错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!