我试图让linq调用存储过程。我知道这通常很简单,但在这种情况下,我使用自己的自定义实体类,我处于死胡同。
以下链接将向您展示我的OM设置
是否有人知道如何从您自己的自定义实体类调用存储过程?
编辑:
忘了两件事。
1)这将是一个silverlight应用程序
2)我在MSDN上累了This,但是我无法调用安全功能execption。
答案 0 :(得分:0)
你的问题有点含糊,所以我不确定这是否会有所帮助,但我只是使用AdventureWorks db进行了一些测试,它似乎有效 - 我会发布我的测试代码,也许它我会告诉你一个线索。 (这只是将Employee表中的3列提取到新的/自定义“EmpTest”类中。)
存储过程:
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
LINQ to SQL存储过程方法(将它放在DataContext类中):
[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));
}
自定义实体:
[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; }
}
}
最后是电话:
EmpTest emp = context.Get_Employee(41).SingleOrDefault();