3

我现在正在使用实体框架开发大型政府应用程序。起初我有一个关于启用 SQL 应用程序角色的问题。使用 ado.net 我正在使用以下代码:

SqlCommand cmd = new SqlCommand("sys.sp_setapprole");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = _sqlConn;
            SqlParameter paramAppRoleName = new SqlParameter();
            paramAppRoleName.Direction = ParameterDirection.Input;
            paramAppRoleName.ParameterName = "@rolename";
            paramAppRoleName.Value = "AppRole";
            cmd.Parameters.Add(paramAppRoleName);

            SqlParameter paramAppRolePwd = new SqlParameter();
            paramAppRolePwd.Direction = ParameterDirection.Input;
            paramAppRolePwd.ParameterName = "@password";
            paramAppRolePwd.Value = "123456";
            cmd.Parameters.Add(paramAppRolePwd);

            SqlParameter paramCreateCookie = new SqlParameter();
            paramCreateCookie.Direction = ParameterDirection.Input;
            paramCreateCookie.ParameterName = "@fCreateCookie";
            paramCreateCookie.DbType = DbType.Boolean;
            paramCreateCookie.Value = 1;
            cmd.Parameters.Add(paramCreateCookie);

            SqlParameter paramEncrypt = new SqlParameter();
            paramEncrypt.Direction = ParameterDirection.Input;
            paramEncrypt.ParameterName = "@encrypt";
            paramEncrypt.Value = "none";
            cmd.Parameters.Add(paramEncrypt);

            SqlParameter paramEnableCookie = new SqlParameter();
            paramEnableCookie.ParameterName = "@cookie";
            paramEnableCookie.DbType = DbType.Binary;
            paramEnableCookie.Direction = ParameterDirection.Output;
            paramEnableCookie.Size = 1000;
            cmd.Parameters.Add(paramEnableCookie);

            try
            {
                cmd.ExecuteNonQuery();
                SqlParameter outVal = cmd.Parameters["@cookie"];
                // Store the enabled cookie so that approle  can be disabled with the cookie.
                _appRoleEnableCookie = (byte[]) outVal.Value;

            }
            catch (Exception ex)
            {
                result = false;
                msg = "Could not execute enable approle proc." + Environment.NewLine + ex.Message;
            }

但是无论我搜索多少,我都找不到在 EF 上实现的方法。

另一个问题是:如何将应用程序角色添加到实体数据模型设计器?


我正在使用下面的代码执行 EF 参数:

AEntities ar = new AEntities();

            DbConnection con = ar.Connection;
            con.Open();
            msg = "";
            bool result = true;
            DbCommand cmd = con.CreateCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            var d = new DbParameter[]{
            new SqlParameter{ ParameterName="@r", Value ="AppRole",Direction =  ParameterDirection.Input}
          , new SqlParameter{ ParameterName="@p", Value ="123456",Direction =  ParameterDirection.Input}
           };
            string sql = "EXEC " + procName + " @rolename=@r,@password=@p";
            var s = ar.ExecuteStoreCommand(sql, d);

当运行 ExecuteStoreCommand 这一行返回错误:

应用程序角色只能在 ad hoc 级别激活。

4

2 回答 2

1

我按照以下方式进行(假设数据库优先):

  1. 我从数据库创建 DbContext 并将其命名为 MyEntitiesBase
  2. 我从 MyEntitiesBase 继承以使用以下代码创建 MyEntities:

    public partial class MyEntities : MyEntitiesBase
    {
    
    private byte[] appRoleCookie;
    
    private void SetAppRole()
    {
        try
        {
            appRoleCookie = Database.SqlQuery<byte[]>(
                @"
                DECLARE @cookie VARBINARY(8000)
                DECLARE @r INT
                EXEC sp_setapprole 'user', 'pass', @fCreateCookie = true, @cookie = @cookie OUTPUT
                SELECT @cookie").First();
        }
        catch
        {
            throw new AuthenticationException();
        }
    }
    
    private void UnSetAppRole()
    {
        bool failed = Database.SqlQuery<bool>("DECLARE @result BIT; EXEC @result = sp_unsetapprole @cookie = " + appRoleCookie.ToHexadecimalString() + "; SELECT @result").First();
        if (failed)
            throw new SecurityException();
    }
    
    public MyEntities() : base()
    {
        Database.Connection.Open();
        SetAppRole();
    }
    
    private bool disposed = false;
    
    protected override void Dispose(bool disposing)
    {
        if (disposed)
            return;
        UnSetAppRole();
        Database.Connection.Close();
        disposed = true;
        base.Dispose(disposing);
    }
    }
    

ToHexadecimalString的扩展方法在哪里IEnumerable<byte>,如下:

public static class BytesExtensions
{
    public static string ToHexadecimalString(this IEnumerable<byte> bytes)
    {
        return "0x" + string.Concat(bytes.Select(b => b.ToString("X2")));
    }
}

就是这样。适用于连接池和一切。您只需使用此继承版本而不是 EF 生成的版本。

于 2017-01-16T15:18:02.390 回答
0

基本上你正在做的是调用一个存储过程。

实体框架具有执行存储过程的功能。这是一个视频解释:http: //msdn.microsoft.com/en-us/data/gg699321.aspx

如果您向下滚动到“使用导入函数映射存储过程”部分,您将找到与您相关的部分。

于 2012-10-25T21:45:43.863 回答