简体   繁体   English

无法在Entity Framework 6中执行存储过程

[英]Unable to execute stored procedure in Entity Framework 6

I have an application that uses EF6. 我有一个使用EF6的应用程序。 I need to execute the stored procedure, so I do the following: 我需要执行存储过程,因此请执行以下操作:

using (SqlConnection conn = new SqlConnection(context.Database.Connection.ConnectionString))
{                      
    SqlCommand cmd = new SqlCommand("dbo.ExampleStoredProcedure", conn)
    {
        CommandType = CommandType.StoredProcedure
    };

    cmd.Parameters.Add(new SqlParameter("@param1", "parameterValue"));
    SqlDataAdapter da = new SqlDataAdapter { SelectCommand = cmd };

    durations = new DataSet();
    da.Fill(durations);                        
}

My connection string from Web.config is: 我的Web.config连接字符串是:

<add name="MyEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer\DEV;initial catalog=MyApp;User Id=UserId;Password=password123;Application Name=Connection Secured;integrated security=false;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

When the code above runs I get an exception: 当上面的代码运行时,我得到一个异常:

Login failed for user 'UserId'.

The exception also contains the SQL error code: 18456 . 该异常还包含SQL错误代码: 18456 I confirmed that user has full access to this SQL server, database, instance and stored procedure by logging into SQL server with this SQL account and running the stored procedure in question. 通过使用此SQL帐户登录到SQL Server并运行相关存储过程,我确认用户具有对该SQL Server,数据库,实例和存储过程的完全访问权限。

I also tried defining a new SqlConnection object: 我也尝试定义一个新的SqlConnection对象:

 SqlConnection newCon = new SqlConnection("Server=MyServer\\DEV;Database=MyApp;User Id=UserId;Password = password123;");

and executing the stored procedure against it - it worked this time. 并针对它执行存储过程-这次它起作用了。 So I do have a workaround, but would like to use the context.Database.Connection.ConnectionString object to avoid having multiple connection string in web.config file. 所以我确实有一种解决方法,但想使用context.Database.Connection.ConnectionString对象来避免在web.config文件中具有多个连接字符串。

Any suggestions? 有什么建议么?

This behaviour is by design for security reasons as this answer to a question suggested. 出于安全原因,此行为是设计使然,因为问题已得到建议。 I added Persist Security Info=True to connection string that Entity Framework generated in web.config file and I was able to reuse context.Database.Connection.ConnectionString to execute the stored procedure with ADO. 我在实体框架在web.config文件中生成的连接字符串中添加了Persist Security Info=True ,并且能够重用context.Database.Connection.ConnectionString来使用ADO执行存储过程。

You don't need to (and shouldn't) open a separate connection for this. 您不需要(也不应该)为此打开单独的连接。 You can use the same connection that the DbContext uses. 您可以使用DbContext使用的相同连接。

Just open the connection (it will be closed when the DbContext is disposed) and cast it to SqlConnection. 只需打开连接(在处理DbContext时它将关闭)并将其强制转换为SqlConnection。 eg 例如

        db.Database.Connection.Open();
        var con = (SqlConnection)db.Database.Connection;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM