是否有更清晰或更优雅的方法使用Entity Framework将多个参数传递给SQL存储过程?

时间:2022-09-25 11:02:36

Right now I am passing multiple parameters to a stored procedure with Entity Framework and it looks like this..

现在我将多个参数传递给具有Entity Framework的存储过程,它看起来像这样。

public long AddDealer(Dealer dealer)
    {
        return Convert.ToInt32(AWJE.Database.SqlQuery<Dealer>(
            "usp_InsertDealer @Name, @Description",
            new SqlParameter("@DealerID", dealer.DealerID).Value,
            new SqlParameter("@Name", dealer.Name),
            new SqlParameter("@Description", dealer.Description)
            ));
    }

is there a more elegant or cleaner way to pass multiple parameters, instead of the way shown? If I come across other stored procedures that have many more parameters to be passed then this way looks like it will get messy real quick.

是否有更优雅或更清晰的方式来传递多个参数,而不是显示的方式?如果我遇到其他存储过程有更多参数要传递,那么这种方式看起来会很快变得混乱。

1 个解决方案

#1


5  

Your code example is clearly not a working example:

你的代码示例显然不是一个有效的例子:

  1. I don't see how you can convert an object of type Dealer to an int.
  2. 我不知道如何将Dealer类型的对象转换为int。
  3. If your method is expected to return a long, then why convert the result of the stored procedure to an int?
  4. 如果你的方法预计会返回一个long,那么为什么要将存储过程的结果转换为int?
  5. You are passing a @DealerID parameter, but it's not part of the SP call.
  6. 您正在传递@DealerID参数,但它不是SP调用的一部分。
  7. Not sure why one of the SqlParameters has a call to .Value add onto it.
  8. 不确定为什么其中一个SqlParameters会调用.Value添加到它上面。

So, let me adjust it, and assume that your starting point is instead something like this:

所以,让我调整它,并假设你的出发点是这样的:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<Dealer>(
        "usp_InsertDealer @Name, @Description",
        new SqlParameter("@Name", dealer.Name),
        new SqlParameter("@Description", dealer.Description)
        ).DealerID;
}

Or maybe you decide that the SP returns a long directly, like this:

或者你可能决定SP直接返回一个long,如下所示:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<long>(
        "usp_InsertDealer @Name, @Description",
        new SqlParameter("@Name", dealer.Name),
        new SqlParameter("@Description", dealer.Description)
        );
}

Either way, the only simplification I see is that you can change the invocation to SqlQuery so that positional parameters are used instead of named parameters. Making that change allows you to forego the creation of explicit SqlParameter instances. The call could then be simplified to this:

无论哪种方式,我看到的唯一简化是您可以将调用更改为SqlQuery,以便使用位置参数而不是命名参数。进行此更改允许您放弃创建显式SqlParameter实例。然后可以将呼叫简化为:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<long>(
        "usp_InsertDealer @Name, @Description",
        dealer.Name,
        dealer.Description
        );
}

... where how you name @Name or @Description no longer has any importance, but you have to make sure you pass in the parameter values in the right order.

...你如何命名@Name或@Description不再具有任何重要性,但你必须确保以正确的顺序传递参数值。

Apart from this, I don't know that you can make this cleaner or more elegant.

除此之外,我不知道你可以使这个更干净或更优雅。

#1


5  

Your code example is clearly not a working example:

你的代码示例显然不是一个有效的例子:

  1. I don't see how you can convert an object of type Dealer to an int.
  2. 我不知道如何将Dealer类型的对象转换为int。
  3. If your method is expected to return a long, then why convert the result of the stored procedure to an int?
  4. 如果你的方法预计会返回一个long,那么为什么要将存储过程的结果转换为int?
  5. You are passing a @DealerID parameter, but it's not part of the SP call.
  6. 您正在传递@DealerID参数,但它不是SP调用的一部分。
  7. Not sure why one of the SqlParameters has a call to .Value add onto it.
  8. 不确定为什么其中一个SqlParameters会调用.Value添加到它上面。

So, let me adjust it, and assume that your starting point is instead something like this:

所以,让我调整它,并假设你的出发点是这样的:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<Dealer>(
        "usp_InsertDealer @Name, @Description",
        new SqlParameter("@Name", dealer.Name),
        new SqlParameter("@Description", dealer.Description)
        ).DealerID;
}

Or maybe you decide that the SP returns a long directly, like this:

或者你可能决定SP直接返回一个long,如下所示:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<long>(
        "usp_InsertDealer @Name, @Description",
        new SqlParameter("@Name", dealer.Name),
        new SqlParameter("@Description", dealer.Description)
        );
}

Either way, the only simplification I see is that you can change the invocation to SqlQuery so that positional parameters are used instead of named parameters. Making that change allows you to forego the creation of explicit SqlParameter instances. The call could then be simplified to this:

无论哪种方式,我看到的唯一简化是您可以将调用更改为SqlQuery,以便使用位置参数而不是命名参数。进行此更改允许您放弃创建显式SqlParameter实例。然后可以将呼叫简化为:

public long AddDealer(Dealer dealer)
{
    return AWJE.Database.SqlQuery<long>(
        "usp_InsertDealer @Name, @Description",
        dealer.Name,
        dealer.Description
        );
}

... where how you name @Name or @Description no longer has any importance, but you have to make sure you pass in the parameter values in the right order.

...你如何命名@Name或@Description不再具有任何重要性,但你必须确保以正确的顺序传递参数值。

Apart from this, I don't know that you can make this cleaner or more elegant.

除此之外,我不知道你可以使这个更干净或更优雅。