views:

61

answers:

1

Hi

Am trying to get linq to call a stored procedure. I know this is usually rather simple, but in this case i am using my own Custom Entity Classes and I am at a dead end.

the link below will show you how I have my OM setup

example

does any one know how you can call a stored procedure from your own Custom Entity Class?

EDIT:

Forgot two things.

1) this will be a silverlight app
2) I have tired This on MSDN but I get cannot call a security function execption.

+1  A: 

Your question is a little vague so I'm not sure if this will help, but I just ran a little test using the AdventureWorks db and it seemed to work - I'll post my test code and maybe it'll give you a clue. (This is just fetching 3 columns from the Employee table into a new/custom "EmpTest" class.)

The stored procedure:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Get_Employee]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Get_Employee]

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE Get_Employee 
    @id int = 0
AS
BEGIN
    SET NOCOUNT ON;

    SELECT EmployeeID, Title, VacationHours from HumanResources.Employee where EmployeeID = @id
END
GO

The LINQ to SQL stored procedure method (put this in your DataContext class):

[Function(Name="dbo.Get_Employee")]
public ISingleResult<EmpTest> Get_Employee([Parameter(DbType="Int")] System.Nullable<int> id)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id);
    return ((ISingleResult<EmpTest>)(result.ReturnValue));
}

The custom entity:

[Table(Name = "HumanResources.Employee")]
public class EmpTest
{
    private int _EmployeeID;
    private string _Title;
    private short _VacationHours;

    [Column(Storage = "_EmployeeID", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int EmployeeID
    {
        get { return this._EmployeeID; }
        set { this._EmployeeID = value; }
    }

    [Column(Storage = "_Title", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
    public string Title
    {
        get { return this._Title; }
        set { this._Title = value; }
    }

    [Column(Storage = "_VacationHours", DbType = "SmallInt NOT NULL")]
    public short VacationHours
    {
        get { return this._VacationHours; }
        set { this._VacationHours = value; }
    }
}

And, finally the call:

EmpTest emp = context.Get_Employee(41).SingleOrDefault();
shaunmartin