Entity Framework SqlFunctions teaches you how to call the SQL Server method function stubs in EF, sqlfunctions
Today, I have studied SqlFunctions for a day. I have consulted some cool people in the group and found that everyone is unfamiliar with this,
Some do not even mean that the methods in SQL Server cannot be called in EF.
Because I have previously worked on SqlMethod in linq to SQL, I think EF must be able.
First, we need to briefly introduce EF6 and EF5. When you use NuGet as an EF5 package, only EntityFramework is available, while EF6 does have EntityFramework and EntityFramework. SqlServer,
The two are very different.
In the EF5 environment, how do we use SqlFunctions?
First, add the EF environment, add Syste. Data. Entity to the reference, and then add the namespace.
using System.Data.Objects.SqlClient;
Then write a controller to test
1 public ActionResult Index() 2 { 3 int Count = 0; 4 using (Models.TestProjectDBEntities db = new Models.TestProjectDBEntities()) 5 { 6 var Query = from c in db.AdminInfoes where SqlFunctions.Square(1.00)==c.Orders select c; 7 Count = Query.ToList().Count(); 8 } 9 ViewBag.Count = Count;10 return View();11 }
Running properly.
In the EF6 environment,
Our references include EntityFramework and EntityFramework. SqlServer,
Then we add the namespace
using System.Data.Entity.SqlServer;
Note that the namespace is different from the namespace in EF5. I also encountered a misunderstanding in this regard. When I added using System. Data. Objects. SqlClient;, I always reported an error,
This error is indeed quite strange. I have not found it for many reasons on the Internet. The last chance is to get this result.
Then we can run the above test code, which should also pass.
SqlFunctions provides many methods, which are relatively simple and will not be described in detail.
This group provides technical support for ASP. net mvc, EF, LINQ, and WEB APIs. It does not care about many people, but about human essence.
ASP. net mvc group 171560784
We invite all experts and beginners to join us.
How to use C #, Entity FrameWork to export the column name of the table in sqlserver to the first column in Excel
1. There will be a field name when you get the data. reflection can be used.
2. There is usually a Config class for the table, that is, the fields of the Round class listed in the database are matched.
How does entity framework connect to the sqlserver2005 database?
For more information, see msdn.microsoft.com/...6.aspx.