Lambda expression for showing table status - entity-framework

I am using ASP.NET Core & Entity Framework Core. How can I write this query as a lambda expression or linq?
show table status WHERE NAME = 'crm_Pipeline';

You cannot. Your only option to execute it is to write:
_context.Database.SqlQuery<Status>("show table status WHERE NAME = 'crm_Pipeline'");
And you need to declare the status class that covers the result.

Related

How to use table valued function in entity framework code first approach?

i am working on a project using Entity Framework and now i got a situation where i need to use table valued function which returns table with 2 columns, hence i searched a lot and i came to know that we use table valued functions in Database first approach while i needed it in Code first.
here is the situation
i have a table with two columns
Table1
Id int PK
priority int
i want to use this table in my query in EF.
Is their any way i can use Table Valued function?
We can do this by using the c# code generated by the CLR for Database first approach
you can refer this url for the full description
http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx
i had used this code and it worked fine for me
[EdmFunction("NorthwindEntities", "GetDetailsForOrder")]
public IQueryable<Order_Detail> GetDetailsForOrder(Nullable<global::System.Int32> oid)
{
ObjectParameter oidParameter;
if (oid.HasValue)
{
oidParameter = new ObjectParameter("Oid", oid);
}
else
{
oidParameter = new ObjectParameter("Oid", typeof(global::System.Int32));
}
return base.CreateQuery<Order_Detail>("[NorthwindEntities].[GetDetailsForOrder](#Oid)", oidParameter);
}
I created a custom model convention which allows using store functions in CodeFirst in EF6.1. The convention is available on NuGet http://www.nuget.org/packages/EntityFramework.CodeFirstStoreFunctions. Here is the link to the blogpost containing all the details: http://blog.3d-logic.com/2014/04/09/support-for-store-functions-tvfs-and-stored-procs-in-entity-framework-6-1/

AsEnumerable with stored procedure using Entity Framework

I am new in using stored procedure using Microsoft Entity Framework. To do some practice I used Northwind database and I was successfully able to do something like this:
var qry = (from row in dbContext.CustOrderHist("custID").AsEnumerable()
select row).Skip(10).Take(10);
Then I have my own DB I am working with it also has a few stored procedures and I similarly used the 'Add Function Import; feature of .Net to create ComplexType to get the stored procedure results (the way I practiced in Northwind Model).
But when I try to do the same:
var qry = (from row in _dbContext.spComplianceReport("SomeID", null).AsEnumerable()
select row).Skip(10).Take(10);
It would not work, and gives error about AsEnumerable(). The following error:
System.Data.Objects.ObjectResult
does not contain a definition for AsEnumerable and no extension method
AsEnumerable accepting a first argument of type
System.Data.Objects.ObjectResult
could be found (are you missing a using directive or an assembly
reference?)
This problem is with my every stored procedure in my Model. What can be different with my Model than the Northwind.
Ok doing the following resolved my problem:
using System.Linq;

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.

Call function in query in Entity framework 3.5

I am trying to run following query in entity framework 3.5
var test = from e in customers where IsValid(e) select e;
Here IsValid function takes current customer and validate against some conditions and returns false or true. But when I am trying to run the query it is giving error "LINQ Method cannot be translated into a store expression." Can any body tell me any other approach?
One approach I can think of is to write all validation conditions here, but that will make the code difficult to read.
Thanks
Ashwani
As the error text implies, the EF Linq provider can not translate the IsValid method into SQL.
If you want the query to execcute in the DB, then you would have to write your validation conditions in the linq statement.
Or if you simply want to fetch all customers and then check if they are valid you could do:
var test = from e in customers.ToList() //execute the query directly w/o conditions
where IsValid(e)
select(e);

Entity framework function import, can't load relations for functions that return entity types

I've created a function import that returns the results of a stored proceedure as one of my entities. however I can't seem to traverse my through navigation properties to access the data in other entities. I know that you can use include() for objectQueries but can't find anything that will force the EF to load my relations for entity results of function imports.
Any ideas??
Thanks in advance.
This is not possible in EF 1.0
The reason is that EF will consider stored procedure values to be just values and not navigation properites.
For example, Employee entity has multiple Order entities. In Order you have a property called EmployeeID. When the database fills your query using include statements, it creates 1 projection query in SQL to populate all of the Order data that a particular Employee could have.
So if I said
var employee = context.Employees.Include("Orders").Where(e => e.ID == 1).First();
var orders = employee.Orders;
The SQL for the first query will create a projection query which will contain orders where the EmployeeID = 1.
Now when your stored procedure runs, this can do any code behind the scenes (in otherwords it can return any set of data). So when SQL runs the stored procedure, it just runs the code in that stored procedure and does not have any knowledge that EmployeeID on Order is an FK to that property. Additionally, if your stored procedure returns an Employee entity, then you are looking at another scenario where you will not even have an OrderID to pursue.
To work around this though, you can setup your query in EF using Include statements that can mirror any stored procedure. If you use the proper mix of .Select and .Include statements you should be able to do the same thing.