【问题标题】:Migrating stored procedure from LINQ to SQL to Entity Framework 6将存储过程从 LINQ 迁移到 SQL 到 Entity Framework 6
【发布时间】:2015-09-26 09:29:04
【问题描述】:

我已将我的项目从 Linq-to-SQL 迁移到 Entity Framework 6。解决了这么多问题后,我终于找到了一个我不知道该怎么办的问题。转换后大约三分之一的单元测试失败,因为存储过程返回标量值just work different way

在 Linq-to-SQL 中,存储过程返回返回值。在 EF 中,它们返回受影响的行数。有一些解决方法需要更改存储过程(通常从RETURN @retvalueSELECT @retvalue)。但这需要对 T-SQL 进行更改。该项目仍然包含一些旧 ASP.NET 的遗留代码,例如 aspnet_Membership_CreateUser 等等。这意味着我无法更改这些存储过程,因为其中一部分由 ASP.NET ADO.NET Membership 提供程序使用,一部分由 Linq-to-SQL 代码使用。我考虑的解决方案是为这些部分遗留代码制作 T-SQL 包装器。

我想保持存储过程不变的另一个原因是,如果升级到 EF 不成功,可能会恢复到以前的提交。 A 不想来回更改数据库。不过这个理由不是那么强,只是因为我的懒惰。

我知道会员提供者是老式的,但我不能在一次提交中重写整个应用程序,我需要保持稳定。

完成迁移的最佳方法是从 EF 调用存储过程,如 Linq-to-SQL,但我没有找到方法。英孚对此感到失望。经过多年的发展,EF 怎么可能不支持从存储过程返回标量值?

你会如何解决这个问题?

【问题讨论】:

    标签: sql-server entity-framework stored-procedures linq-to-sql


    【解决方案1】:

    你好 Qub1n,

        // Create the gender parameter var param1 = new SqlParameter {
        ParameterName = "@userId",
        Value = _userId };
    
        // Create the return code var returnValue = new SqlParameter {
        ParameterName = "@ReturnCode",
        SqlDbType = System.Data.SqlDbType.Int,
        Direction = System.Data.ParameterDirection.Output };
    
    
        // Gender is an int column in the database.
        _context
        .Database
        .ExecuteSqlCommand("exec @ReturnCode = GetGenderOfUser     @userId", 
        param1,    returnValue);
    
        return (int)returnValue.Value;
    

    这对我有用。我的存储过程是这样的:

    CREATE PROCEDURE [dbo].[GetGenderOfUser] 
        -- Add the parameters for the stored procedure here
        @userId uniqueidentifier 
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        declare @retval int
        -- Insert statements for procedure here
        set @retval = (select top(1) Gender from Users where Guid = @userId)
    
        return @retval
    END
    

    让我知道这是否有效!

    【讨论】:

    • 最后我得到了上面发布的解决方案。 ExecuteSqlCommand 可以工作,但我无法从数据库自动生成/更新模型。
    【解决方案2】:

    最后我得到了包装器,它保留了数据库的原始功能(用于 ASP.NET MembershipProvider)并为 EntityFramework 创建存储过程的版本:

    CREATE PROCEDURE [dbo].[aspnet_UsersInRoles_AddUsersToRolesEF]
        @ApplicationName  nvarchar(256),
        @UserNames        nvarchar(4000),
        @RoleNames        nvarchar(4000),
        @CurrentTimeUtc   datetime
    AS
    BEGIN
        DECLARE @return_value int
    
    EXEC    @return_value = [dbo].[aspnet_UsersInRoles_AddUsersToRoles]
            @ApplicationName,
            @UserNames,
            @RoleNames,
            @CurrentTimeUtc
    
    SELECT  @return_value
    END
    
    GO
    

    现在我可以使用编译器检查存储过程中参数的数量和类型。

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 2017-10-12
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多