如何在实体框架中使用表值函数的第一种方法? [英] How to use table valued function in entity framework code first approach?
问题描述
这里是情况
我有一个表与两列
Table1
Id int PK
priority int
我想在EF中的查询中使用此表。
我们可以通过使用CLR生成的c#代码来做到这一点,我们可以使用Table Value的功能吗?
数据库第一种方法
你可以参考这个url的完整描述
http://blogs.msdn.com/b/adonet/archive/2011/06/30/演练-TA有价值的功能-June-ctp.aspx
我已经使用了这个代码,它对我来说很好。
[EdmFunction(NorthwindEntities,GetDetailsForOrder)]
public IQueryable< Order_Detail> GetDetailsForOrder(Nullable< global :: System.Int32> oid)
{
ObjectParameter oidParameter;
if(oid.HasValue)
{
oidParameter = new ObjectParameter(Oid,oid);
}
else
{
oidParameter = new ObjectParameter(Oid,typeof(global :: System.Int32));
}
return base.CreateQuery< Order_Detail>([NorthwindEntities]。[GetDetailsForOrder](@ Oid),oidParameter);
}
i am working on a project using Entity Framework and now i got a situation where i need to use table valued function which returns table with 2 columns, hence i searched a lot and i came to know that we use table valued functions in Database first approach while i needed it in Code first.
here is the situation
i have a table with two columns
Table1
Id int PK
priority int
i want to use this table in my query in EF.
Is their any way i can use Table Valued function?
We can do this by using the c# code generated by the CLR for Database first approach
you can refer this url for the full description http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx
i had used this code and it worked fine for me
[EdmFunction("NorthwindEntities", "GetDetailsForOrder")]
public IQueryable<Order_Detail> GetDetailsForOrder(Nullable<global::System.Int32> oid)
{
ObjectParameter oidParameter;
if (oid.HasValue)
{
oidParameter = new ObjectParameter("Oid", oid);
}
else
{
oidParameter = new ObjectParameter("Oid", typeof(global::System.Int32));
}
return base.CreateQuery<Order_Detail>("[NorthwindEntities].[GetDetailsForOrder](@Oid)", oidParameter);
}
这篇关于如何在实体框架中使用表值函数的第一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!