EF: SQL function as a property

Is it possible to add the scalar value of an SQL function as a property in EF?

I have some functions that summarize different values ​​in an OrderLine table that accept OrderId (i.e. SUM of OrderLineCost).

I would like to have access to them through the order data model.

Is there a way to add these functions as properties in the order data model so that I can simply access Order.OrderLineCostTotal instead of making individual calls to the database?

thank

+4
source share
1 answer

Yes. You have to do it. With EF 6.1, it’s easier to create things in both the first and the first database.

codeplex Code first store functions, . nuget:

(, , ) Entity Framework 6.1.1+ Code First.

https://codefirstfunctions.codeplex.com/

, , SQL. readonly:

[DbFunction("MyContext", "CustomersByZipCode")]
public IQueryable<Customer> CustomersByZipCode(string zipCode)
{
    var zipCodeParameter = zipCode != null ?
        new ObjectParameter("ZipCode", zipCode) :
        new ObjectParameter("ZipCode", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext
        .CreateQuery<Customer>(
            string.Format("[{0}].{1}", GetType().Name, 
                "[CustomersByZipCode](@ZipCode)"), zipCodeParameter);
}

EF 6, import, . , ( Visual Studio 2013), EDMX xml. :

http://logiczone.me/entity-framework-and-sql-db-scalar-functions

+3

All Articles