2013-02-08 44 views
2

我有一個模型優先的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 [email protected]) 
     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); 

智能感知說我使用的「上下文」是錯誤的......這是一個「變量「,我將它用作」方法「。

此外,currPOID被拒絕,因爲它正在尋找system.data.objects.OjbectParameter,而不是int。 Intellisense很滿意功能名稱和其他參數(奇怪的是...)

我在做什麼錯在這裏?

+0

你可能想要發表更多的代碼。我們不知道什麼類型的背景是 – Mario 2013-02-08 18:09:00

+0

不得不說,「編輯」是趕上了趕上可以。我很抱歉代碼塊沒有正確顯示。我不知道如何解決它。 – 2013-02-08 18:22:48

回答

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(); 
    } 
} 
0

如果你想一個面向對象的方法,那麼Mindless passenger有一個項目,允許你調用從這樣的實體框架的工作一個存儲過程。 ...

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) 
    { 
    } 
} 

如果這兩種方法中的任何一種都有用?