在实体框架中使用存储过程

时间:2013-02-08 18:01:04

标签: c# entity-framework stored-procedures

我有一个模型优先的EF模型。我刚刚导入了第一个存储过程:cpas_POIDVendorProjectDate

我将它作为一个函数导入。它有三个输入参数:@ProjectID(int)@VendorID(int)@Workdate(datetime),并返回@POID(int)

这是SQL代码:

 CREATE PROCEDURE [dbo].[cpas_POIDVendorProjectDate]
    @VendorID int,
    @ProjectID int,
    @WorkDate datetime,
    @PO_ID int OUTPUT
 AS
 BEGIN
    SET NOCOUNT ON;

    DECLARE @RowCount int;

       SELECT @PO_ID = ID FROM tblPO WHERE 
        VendorID = @VendorID
        AND ExpirationDate >= @WorkDate
        AND (ProjectID IS NULL OR ProjectID = @ProjectID)
        AND CapitalExpense = (
          SELECT CapitalExpense FROM tblProjects WHERE ID=@ProjectID)
        AND GroupCode in (1,3,5);

     SET @RowCount = @@RowCount;

     IF (@RowCount != 1)
       SET @PO_ID = -1*@RowCount;

END

我在c#程序中调用它如下:

context.cpas_POIDVendorProjectDate(
    currVendorID, currProjectID, currWorkDate, currPOID);

Intellisense说我使用" context"是错的......它是一个"变量",我将它用作"方法"。

此外,currPOID被拒绝,因为它正在寻找system.data.objects.OjbectParameter,而不是int。 Intellisense对函数名称和其他参数感到满意(奇怪的是......)

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

如果没有别的办法,你可以随时这样做:

using(var context = new MyDataContext())
{
    using(var cmd = context.Database.Connection.CreateCommand())
    {
        cmd.CommandText = "cpas_POIDVendorProjectDate";
        cmd.CommandType = CommandType.StoredProcedure;
        //if the stored proc accepts params, here is where you pass them in
        cmd.Parameters.Add(new SqlParameter("VendorId", 10));
        cmd.Parameters.Add(new SqlParameter("ProjectId", 12));
        cmd.Parameters.Add(new SqlParameter("WorkDate", DateTimw.Now));
        var poid = (int)cmd.ExecuteScalar();
    }
}

答案 1 :(得分:0)

如果你想要一个面向对象的方式,那么Mindless passenger有一个项目,允许你从实体框架中调用存储的proc工作....

using (testentities te = new testentities())
{
    //-------------------------------------------------------------
    // Simple stored proc
    //-------------------------------------------------------------
    var parms1 = new testone() { inparm = "abcd" };
    var results1 = te.CallStoredProc<testone>(te.testoneproc, parms1);
    var r1 = results1.ToList<TestOneResultSet>();
}

...我正在处理一个stored procedure frameworkhere),您可以像我在下面显示的测试方法中那样调用...

[TestClass]
public class TenantDataBasedTests : BaseIntegrationTest
{
    [TestMethod]
    public void GetTenantForName_ReturnsOneRecord()
    {
        // ARRANGE
        const int expectedCount = 1;
        const string expectedName = "Me";

        // Build the paraemeters object
        var parameters = new GetTenantForTenantNameParameters
        {
            TenantName = expectedName
        };

        // get an instance of the stored procedure passing the parameters
        var procedure = new GetTenantForTenantNameProcedure(parameters);

        // Initialise the procedure name and schema from procedure attributes
        procedure.InitializeFromAttributes();

        // Add some tenants to context so we have something for the procedure to return!
        AddTenentsToContext(Context);

        // ACT
        // Get the results by calling the stored procedure from the context extention method 
        var results = Context.ExecuteStoredProcedure(procedure);

        // ASSERT
        Assert.AreEqual(expectedCount, results.Count);
    }
}

internal class GetTenantForTenantNameParameters
{
    [Name("TenantName")]
    [Size(100)]
    [ParameterDbType(SqlDbType.VarChar)]
    public string TenantName { get; set; }
}

[Schema("app")]
[Name("Tenant_GetForTenantName")]
internal class GetTenantForTenantNameProcedure
    : StoredProcedureBase<TenantResultRow, GetTenantForTenantNameParameters>
{
    public GetTenantForTenantNameProcedure(
        GetTenantForTenantNameParameters parameters)
        : base(parameters)
    {
    }
}

如果这两种方法中的任何一种都有好处吗?