如何在实体框架中使用表值函数的第一种方法? [英] How to use table valued function in entity framework code first approach?

查看:121
本文介绍了如何在实体框架中使用表值函数的第一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework进行一个项目,现在我有一个情况,我需要使用表格函数,它返回2列的表,所以我搜索了很多,我知道我们使用表值函数数据库第一个方法,而我需要它在代码第一。



这里是情况



我有一个表与两列

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆