通过调用实体框架从C#标量函数4.0 /的.edmx [英] Calling scalar function from c# using Entity Framework 4.0 / .edmx

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

问题描述

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

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);



我试图添加图像来显示我的意思,但因为我的名声仍然偏低,我不能这样做。然而,该映像可以在这里找到:的http:// social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/756865e5-ff25-4f5f-aad8-fed9d741c05d

感谢。

推荐答案

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

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.

您也可以编辑EDMX文件,以使EDMX生成标量值功能适当的方法,但是如果你会同步你的数据库建模它会被删除。

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.

写标量值函数实现自己:

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

或编辑EDMX和标量值函数的自定义马平添加XML:

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

这个信息是在此的博客文章

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

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