使用Entity Framework 4.0 / .edmx从c#调用标量函数 [英] Calling scalar function from c# using Entity Framework 4.0 / .edmx

查看:155
本文介绍了使用Entity Framework 4.0 / .edmx从c#调用标量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的标量函数映射到我的.edmx,但是它失败了。我右键单击我的实体框架映射,并从数据库中选择更新模型。它出现在我的模型浏览器的存储过程文件夹中。



但是,当我想将它添加到我的函数导入文件夹在模型浏览器中,消息标量函数不会出现显示在下拉列表中。有人可以帮助我吗?



我可以使用旧的方式调用标量函数,例如:

  dbContext.ExecuteStoreQuery< DateTime?>(
SELECT dbo.getMinActualLoadDate({0},{1},{2})AS MyResult,
LoadPkid,LoadFkStartLoc ,TripSheetPkid).First();

但这不是最好的方法。我的经理希望我找到一种能够将标量函数放在函数导入文件夹中的方式,所以我可以使用以下代码而不是以前的代码调用标量函数:

  dbContext.ExecuteFunction(getMinActualLoadDate,paramList); 

我试图添加一个图像来显示我的意思,但是由于我的声誉还是很低,我不能这样做但是,可以在此处找到该图像: http:// social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/756865e5-ff25-4f5f-aad8-fed9d741c05d



谢谢。 p>

解决方案

我遇到同样的问题。这里是解决方案,我发现我的自己足够(在EF5中测试,但也应该在EF4中工作):



不支持映射标量值函数开箱即用,但您可以直接执行。



您还可以编辑edmx文件,使edmx为标量值函数生成正确的方法,但如果您同步数据库模型,则会被删除。



自己编写标量值函数实现:

  string sqlQuery =SELECT [dbo]。[CountMeals]({0}); 
Object [] parameters = {1};
int activityCount = db.Database.SqlQuery< int>(sqlQuery,parameters).FirstOrDefault();

或编辑edmx并添加Xml以定制定标标量函数:

 < Function Name =CountActivitiesAggregate =falseBuiltIn =falseNiladicFunction =falseIsComposable = falseParameterTypeSemantics =AllowImplicitConversionSchema =dbo> 
< CommandText>
SELECT [dbo]。[CountActivities](@personId)
< / CommandText>
< Parameter Name =personIdType =intMode =In/>
< / Function>

此信息已在此 blog post


I would like to map my scalar function to my .edmx but it fails. I right click on my entity framework mapping, and choose update model from database. It appears in my stored procedures folder in my model browser.

However, when I want to add it to my Function Imports folder in the model browser, the message scalar function does not appear shows in the drop down list. Can someone help me?

I can call the scalar function using the old way, such as:

dbContext.ExecuteStoreQuery<DateTime?>(
"SELECT dbo.getMinActualLoadDate ({0}, {1}, {2}) AS MyResult", 
LoadPkid, LoadFkStartLoc, TripSheetPkid).First();

but it is not the best way. My manager would like me to find a way be able to put the scalar function in the "function import" folder so I can call the scalar function using the following code instead of the previous code:

dbContext.ExecuteFunction("getMinActualLoadDate ", paramList);

I tried to add an image to display what I mean but as my reputation is still low, I am unable to do so. However the image could be found here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/756865e5-ff25-4f5f-aad8-fed9d741c05d

Thanks.

解决方案

I've encountered same problem. And here is solution I've found my self suitable enough (tested in EF5, but should also work in EF4):

There is no support of mapping scalar-value functions out of the box but you can execute them directly.

You can also edit edmx file to make edmx generate proper method for scalar-value function, but it ll be deleted if you ll synch you model with database.

Write scalar-valued function implementation yourself:

string sqlQuery = "SELECT [dbo].[CountMeals] ({0})";
Object[] parameters = { 1 };
int activityCount = db.Database.SqlQuery<int>(sqlQuery, parameters).FirstOrDefault();

Or edit edmx and add Xml for custom maping of scalar-valued function:

<Function Name="CountActivities" Aggregate="false" BuiltIn="false"    NiladicFunction="false" IsComposable="false"   ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <CommandText>
        SELECT [dbo].[CountActivities] (@personId)
    </CommandText>
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>

This information was found in this blog post

这篇关于使用Entity Framework 4.0 / .edmx从c#调用标量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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