【问题标题】:The ConnectionString property has not been initialized executing for second time asp.net coreConnectionString 属性尚未初始化第二次执行 asp.net 核心
【发布时间】:2020-09-21 00:07:08
【问题描述】:

我遇到了一个奇怪的问题,当我第二次执行下面的代码时出现异常

此方法存在于 DB 上下文中(实体框架核心)

代码:

public IEnumerable<T> ExecuteStoredProcedure<T>(string spname, params object[] ps)
{
    string json = string.Empty;
    using (var sqlConnection = (SqlConnection)this.Database.GetDbConnection())
    {
        var command = sqlConnection.CreateCommand();
        command.CommandText = spname;
        command.CommandType = System.Data.CommandType.StoredProcedure;

        for (int i = 0; i < ps.Length; i++)
        {
            var obj = ps[i];
            System.Reflection.PropertyInfo nameprop = obj.GetType().GetProperty("name");
            string name = (string)(nameprop.GetValue(obj, null));

            System.Reflection.PropertyInfo valprop = obj.GetType().GetProperty("value");
            dynamic val = (dynamic)(valprop.GetValue(obj, null));

            command.Parameters.Add(new SqlParameter(name, val));
        }
        this.Database.OpenConnection();
        using (var result = command.ExecuteReader())
        {
            var rawdata = Serialize(result);
            json = JsonConvert.SerializeObject(rawdata, Formatting.Indented);
        }
    }
    
    return JsonConvert.DeserializeObject<IEnumerable<T>>(json);
}

例外

System.InvalidOperationException: 'The ConnectionString property has not been initialized.'

【问题讨论】:

  • 你为什么不let EntityFramework do the work?这将比在 json 之间转换来回高效得多,而且更不容易出错
  • 非常类似于这个问题:stackoverflow.com/q/10911635/5803406
  • @AndrewWilliamson 因为 EF Core 无法执行与 dbset 无关的存储过程
  • @devNull 实际上是的,我没注意,但我在 EF Core 中添加了一个与 dbset 无关的动态执行存储过程,它是完全动态的
  • @IssaSaman 你知道哪些存储过程在编译时可用,还是动态的?

标签: c# .net entity-framework asp.net-core entity-framework-core


【解决方案1】:

我已经通过删除解决了这个问题

public IEnumerable<T> ExecuteStoredProcedure<T>(string spname, params object[] ps)
{
    string json = string.Empty;
    var sqlConnection = (SqlConnection)this.Database.GetDbConnection();
    var command = sqlConnection.CreateCommand();
    command.CommandText = spname;
    command.CommandType = System.Data.CommandType.StoredProcedure;

    for (int i = 0; i < ps.Length; i++)
    {
        var obj = ps[i];
        System.Reflection.PropertyInfo nameprop = obj.GetType().GetProperty("name");
        string name = (string)(nameprop.GetValue(obj, null));


        System.Reflection.PropertyInfo valprop = obj.GetType().GetProperty("value");
        dynamic val = (dynamic)(valprop.GetValue(obj, null));

        command.Parameters.Add(new SqlParameter(name, val));
    }
    this.Database.OpenConnection();
    var result = command.ExecuteReader();
    var rawdata = Serialize(result);
    json = JsonConvert.SerializeObject(rawdata, Formatting.Indented);

    return JsonConvert.DeserializeObject<IEnumerable<T>>(json);
}

当您离开“使用”块时,.NET 运行时将为您调用变量的 Dispose() 方法。所以到第二次调用该方法时,您的静态连接变量不再有效。

尝试从你的方法中删除 using 语句,你会没事的。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/ae6780da-7ad6-4e90-977e-7b0a71345be8/the-connectionstring-property-has-not-been-initialized-random-error?forum=csharpgeneral

【讨论】:

  • 删除使用是错误的。您需要使用 using 块来防止 CPU 和内存的高使用率。尝试使用 sqlConnection.Open() 而不是 this.Database.OpenConnection()。
  • @Asherguru 我试过你所说的,需要注意的是它是一个连接和相同的上下文,所以没有高使用率
  • 如果有很多用户同时使用您的网站,例如 5000 多个用户,除非您注入了依赖项,否则您将获得高使用率。我以前经历过,我也使用了一个连接和相同的上下文。我只是告诉你,这样你就不会犯和我一样的错误了。
  • @Asherguru 你能解释一下吗?我为此使用了依赖注入
  • 哦,你使用了依赖注入。那么你的就好了。
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
相关资源
最近更新 更多