使用连接到 2 个数据库的 MVC 应用程序在特定数据库上调用存储过程

Call Stored Procedure on specific database with MVC Application connecting to 2 databases

我有 ASP.NET MVC 应用程序 运行 多个数据库,其中一个包含存储过程。在我在 web.config

中添加另一个数据库连接字符串之前,我的应用程序和存储过程工作正常

Web.config:

<connectionStrings>
    <add name="AppDbConnection" connectionString="xyz" providerName="System.Data.SqlClient" />
    <add name="CIDDbConnection" connectionString="abc" providerName="System.Data.SqlClient" />
</connectionStrings>

我在 AppDbConnection

上存储了过程

现在出现错误

Could not find stored procedure 'GetAllFunction'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Could not find stored procedure 'GetAllFunction'.

C#代码调用存储过程:

public List<GetAllFunction_SP_Map> GetAllFunction_From_StoreProcedure()
{
        List<GetAllFunction_SP_Map> query;

        using (var dbContext = new FunctionContext())
        {
            query = dbContext.Database.SqlQuery<GetAllFunction_SP_Map>("exec GetAllFunction").ToList();
        }

        return query;
}

DbContext:

public class FunctionContext : BaseContext<FunctionContext>
{
    public System.Data.Entity.DbSet<App.DAL.Model.GetAllFunction_SP_Map> GetAllFunction_SP_Map { get; set; }
}

我在单独的应用程序文件夹中有两个数据库的数据访问,并且都有它们的 DbContext

我的问题是如何解决这个错误,我相信我需要在 C# 中以某种方式告诉我从我调用存储过程的地方使用特定的数据库。

非常感谢

仅添加连接字符串不会导致此问题。我想,除此之外,您的 FunctionContext 也确实进行了修改,以使用新的连接字符串。试试这个:

using (var dbContext = new FunctionContext("AppDbConnection"))
{
    query = dbContext.Database.SqlQuery<GetAllFunction_SP_Map>("exec GetAllFunction").ToList();
}

...不知道还可以修改什么。