加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 应用程序中断访问dbcontext,Asp .net核心web ap

发布时间:2020-12-16 07:42:23 所属栏目:asp.Net 来源:网络整理
导读:我用EntityFrameworkCore.SqlServer 2.0开发了asp .net核心wep api 2.0应用程序.它是使用数据库第一种方法开发的.当尝试使用dbcontext访问实体时,应用程序将进入中断模式.我找不到应用程序状态进入中断状态的原因.请帮忙解决这个问题. 下面是DBContext类中的
我用EntityFrameworkCore.SqlServer 2.0开发了asp .net核心wep api 2.0应用程序.它是使用数据库第一种方法开发的.当尝试使用dbcontext访问实体时,应用程序将进入中断模式.我找不到应用程序状态进入中断状态的原因.请帮忙解决这个问题.

下面是DBContext类中的OnConfiguring方法.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=(local);Database=VotingAppDB;User ID=sa;Password=123;");
        }
    }

下面的代码块用于访问控制器中的dbcontext实体

// GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        VotingAppDBContext context = new VotingAppDBContext();
        var questions = context.Questions.ToList();
        return new string[] { "value1","value2" };
    }

Installed packages

Application error

解决方法

1.-首先你不应该在控制器内部创建一个上下文,避免使用带有依赖关系的’new’,因为这会使你的代码不可测试,在我的情况下因为我使用UnitOfWork我将它注入IUnitOfWork实例,实际上,MyConext的扩展,你将它注入StartUp类……为此,我有一个私有方法(在一次私人调用中执行此操作),如下所示:

private void AddEntityFrameworkAndDbContext(IServiceCollection services)
????????{
????????????services.AddEntityFrameworkSqlServer();

var migrationsAssemblyName = typeof(MyContext).GetTypeInfo().Assembly.GetName().Name;
        services.AddDbContext<MyContext>(options =>
        {
            options.UseSqlServer(
                "MY CONNECTION STRING GOES HERE (BUT I RETREIVE IT FROM ANOTHER SERVICE)",sqlServerOptionsAction: sqlOptions =>
                {
                    sqlOptions.MigrationsAssembly(migrationsAssemblyName);
                    sqlOptions.EnableRetryOnFailure(maxRetryCount: 5,maxRetryDelay: TimeSpan.FromSeconds(30),errorNumbersToAdd: null);
                });
        },ServiceLifetime.Scoped  // Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request)
               ).AddUnitOfWork<MyContext>(); // This is because I'm also using EF Core Unit of work NuGet Package
    }

我在StartUp类中调用了来自ConfigureServices(IServiceCollection服务)的私有方法,正如我所说的那样

// Add EF,and UoW
        AddEntityFrameworkAndDbContext(services);

2.-其次(但我会说这是你真正的问题)我会说你错过了基础.OnConfiguring(选项);在你的上下文中,它应该是这样的:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=(local);Database=VotingAppDB;User ID=sa;Password=123;");
        }
        base.OnConfiguring(optionsBuilder);
    }

另外,请看看我几周前写的这个答案:
How to setup EF6 Migrations with ASP.NET Core

此外,UnitOfWork项目值得一读,请在此处查看:https://github.com/arch/UnitOfWork

我希望它有所帮助,

胡安

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读