表值函数在EF 5?

时间:2021-05-06 18:02:09

Apologies if this isnt phrased very well, but after upgrading to VS2012/.NET 4.5, I know Table Valued Functions are possible in Entity Framework 5.

如果这句话表达得不太好,我很抱歉,但是在升级到VS2012/之后。NET 4.5,我知道表值函数在实体框架5中是可能的。

We use a custom datalayer / orm, and I cant find any code examples that dont use the EDMX model generator as this is of no use.

我们使用自定义的datalayer / orm,我找不到任何不使用EDMX模型生成器的代码示例,因为这是没有用的。

As a very wild guess I would say some code that defines the table value function will need adding in OnModelCreating(DbModelBuilder modelBuilder) .

作为一种非常疯狂的猜测,我认为需要在onmodelcreation (DbModelBuilder modelBuilder modelBuilder)中添加一些定义表值函数的代码。

Any help appreciated.

任何帮助表示赞赏。

3 个解决方案

#1


6  

Table valued function are not supported for Code-First strategy, only for Database-First with EDMX: http://msdn.microsoft.com/en-us/hh859577. Quote:

表值函数不支持代码优先策略,只支持Database-First与EDMX: http://msdn.microsoft.com/en-us/hh859577。引用:

TVFs are currently only supported in the Database First workflow.

目前,TVFs只支持数据库的第一个工作流。

#2


2  

I was able to easily execute a Table Valued Function using EF 5 as follows:

我可以轻松地使用EF 5执行表值函数,如下所示:

int orderID = 100;
var query = context.Database.SqlQuery<Product>("Select * from [dbo].[tfn_GetOrderProducts](@p0)", orderID);
var results = query.ToList();

where Product can be any POCO class whose member names match up with the results of the table valued function.

其中Product可以是任何POCO类,其成员名称与表值函数的结果匹配。

This isn't a perfect solution -- it's not returning an IQueryable, so you can't use this as part of a larger LINQ query; however, in this case, it was all I needed.

这并不是一个完美的解决方案——它没有返回一个IQueryable,所以您不能将它用作更大的LINQ查询的一部分;然而,在这种情况下,这就是我所需要的。

#3


-1  

the following suggestion was removed from this link http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx. I've found this information that may be usefull to you.

以下建议从这个链接http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx中删除。我找到了这个可能对你有用的信息。

Code First Approach

代码首先方法

Entity Framework June 2011 CTP does not include Code First support for TVFs. However, you can use DbContext against your TVFs. You can do this by adding the DbContext template to your model. The steps to add the template are the following:

实体框架2011年6月CTP不包含对TVFs的代码优先支持。但是,您可以使用DbContext来对抗您的TVFs。您可以通过将DbContext模板添加到模型中来实现这一点。添加模板的步骤如下:

  • Open NorthwindModel.edmx and right click on the canvas
  • NorthwindModel开放。edmx右击画布
  • Click on Add Code Generation Item…
  • 单击Add Code Generation项…
  • Select ADO.NET DbContext Generator V4.2, enter a name for your template, and click Add
  • 选择ADO。NET DbContext生成器V4.2,为模板输入一个名称,然后单击Add

#1


6  

Table valued function are not supported for Code-First strategy, only for Database-First with EDMX: http://msdn.microsoft.com/en-us/hh859577. Quote:

表值函数不支持代码优先策略,只支持Database-First与EDMX: http://msdn.microsoft.com/en-us/hh859577。引用:

TVFs are currently only supported in the Database First workflow.

目前,TVFs只支持数据库的第一个工作流。

#2


2  

I was able to easily execute a Table Valued Function using EF 5 as follows:

我可以轻松地使用EF 5执行表值函数,如下所示:

int orderID = 100;
var query = context.Database.SqlQuery<Product>("Select * from [dbo].[tfn_GetOrderProducts](@p0)", orderID);
var results = query.ToList();

where Product can be any POCO class whose member names match up with the results of the table valued function.

其中Product可以是任何POCO类,其成员名称与表值函数的结果匹配。

This isn't a perfect solution -- it's not returning an IQueryable, so you can't use this as part of a larger LINQ query; however, in this case, it was all I needed.

这并不是一个完美的解决方案——它没有返回一个IQueryable,所以您不能将它用作更大的LINQ查询的一部分;然而,在这种情况下,这就是我所需要的。

#3


-1  

the following suggestion was removed from this link http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx. I've found this information that may be usefull to you.

以下建议从这个链接http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx中删除。我找到了这个可能对你有用的信息。

Code First Approach

代码首先方法

Entity Framework June 2011 CTP does not include Code First support for TVFs. However, you can use DbContext against your TVFs. You can do this by adding the DbContext template to your model. The steps to add the template are the following:

实体框架2011年6月CTP不包含对TVFs的代码优先支持。但是,您可以使用DbContext来对抗您的TVFs。您可以通过将DbContext模板添加到模型中来实现这一点。添加模板的步骤如下:

  • Open NorthwindModel.edmx and right click on the canvas
  • NorthwindModel开放。edmx右击画布
  • Click on Add Code Generation Item…
  • 单击Add Code Generation项…
  • Select ADO.NET DbContext Generator V4.2, enter a name for your template, and click Add
  • 选择ADO。NET DbContext生成器V4.2,为模板输入一个名称,然后单击Add