Entity Framework SqlFunctions 教你如何在EF调用sqlserver方法
今天算是研究了一天的SqlFunctions,请教了几个群的牛人,居然发现大伙对这个都比较陌生, 有的甚至直指EF中是不能调用sqlserver里的方法的。 因为之前搞过linq to sql 里面的SqlMethod ,所以觉得EF里面必须是可以的。 首先需要简短介绍一下EF6和EF5,当你N
今天算是研究了一天的SqlFunctions,请教了几个群的牛人,居然发现大伙对这个都比较陌生,
有的甚至直指EF中是不能调用sqlserver里的方法的。
因为之前搞过linq to sql 里面的SqlMethod ,所以觉得EF里面必须是可以的。
首先需要简短介绍一下EF6和EF5,当你NuGet一个EF5的包的时候,只有EntityFramework,而EF6确有EntityFramework和EntityFramework.SqlServer,
这2者是有很大区别的。
在EF5环境下,我们如何使用SqlFunctions 呢?
首先添加EF环境,在引用中添加Syste.Data.Entity,再添加命名空间
<span>using</span> System.Data.Objects.SqlClient;
然后写一个控制器测试
<span> 1</span> <span>public</span><span> ActionResult Index() </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span> Count = <span>0</span><span>; </span><span> 4</span> <span>using</span> (Models.TestProjectDBEntities db = <span>new</span><span> Models.TestProjectDBEntities()) </span><span> 5</span> <span> { </span><span> 6</span> <span>var</span> Query = <span>from</span> c <span>in</span> db.AdminInfoes <span>where</span> SqlFunctions.Square(<span>1.00</span>)==c.Orders <span>select</span><span> c; </span><span> 7</span> Count =<span> Query.ToList().Count(); </span><span> 8</span> <span> } </span><span> 9</span> ViewBag.Count =<span> Count; </span><span>10</span> <span>return</span><span> View(); </span><span>11</span> }
运行正常。
EF6环境下,
我们的引用中是存在EntityFramework和EntityFramework.SqlServer的,
然后我们添加命名空间
<span>using</span> System.Data.Entity.SqlServer;
注意一下,跟EF5下的命名空间是不一样的,这个地方我也是出现了误区,当我的添加是using System.Data.Objects.SqlClient; 的时候一直报错,
这个错误确实比较奇特,我网上找了很多原因一直没有找到,最后一个偶然的机会,才得到这个结果。
然后我们再运行上面的测试代码,应该也是运行通过的。
SqlFunctions里提供了很多的方法,这个都是比较简单的,就不做详细介绍了。<br><br>本群提供ASP.NET MVC,EF,LINQ,WEB API技术支持,不在乎人多,在乎人精。<br>ASP.NET MVC群 171560784 <br>诚邀各路高手、初学者加入。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks

What is the 0xc0000135 error and why do I get it? According to official Microsoft documentation, the 0xc0000135 error code is related to .NetFramework issues. It seems that many applications that require .NetFramework3.5 to work do not work with the latest Windows 11 update. This is what causes the 0xc0000135 error code and you can resolve this issue by enabling .NetFramework3.5 on your PC. Most modern applications rely on the .NetFramework.dll file to run as expected in the background. but

Microsoft just fixed the safe mode issue in Windows 11, but the operating system is apparently experiencing further issues with the latest cumulative update. KB5012643, an optional update that contains a number of fixes, is crashing applications that use certain components of the .NET 3.5 framework. If you've been using Microsoft's desktop operating systems for a long time, you may have noticed .NET Framework. .NET Framework may appear in Windows updates or during the installation of certain applications. Many applications rely on the .NET Framework to function properly as it contains the
