zzzprojects / EntityFrameworkExtras

EntityFrameworkExtras provides some useful additions to EntityFramework such as executing Stored Procedures with User-Defined Table Types and Output Parameters.
https://entityframework-extras.net/
MIT License
80 stars 40 forks source link
c-sharp efcore entity-framework entity-framework-core entityframework entityframeworkcore

Library Powered By

This library is powered by Entity Framework Extensions

Entity Framework Extensions


What's EntityFrameworkExtras?

EntityFrameworkExtras provides some useful additions to EntityFramework, such as executing Stored Procedures with User-Defined Table Types and Output Parameters.

Executing a Stored Procedure with a User Defined Table Type

[StoredProcedure("storedproc_AddMemberWithAddresses")]
public class AddMemberStoredWithAddressesProcedure
{
        [StoredProcedureParameter(SqlDbType.NVarChar, ParameterName = "ForeName")]
    public string FirstName { get; set; }

    [StoredProcedureParameter(SqlDbType.NVarChar,ParameterName = "SurName")]
    public string LastName { get; set; }

    [StoredProcedureParameter(SqlDbType.Int)]
    public int Age { get; set; }

    [StoredProcedureParameter(SqlDbType.Udt)]
    public List<Address> Addresses { get; set; }
}
[UserDefinedTableType("udt_Address")]
public class Address
{
    [UserDefinedTableTypeColumn(1)]
    public string Line1 { get; set; }

    [UserDefinedTableTypeColumn(2)]
    public string Line2 { get; set; }

    [UserDefinedTableTypeColumn(3)]
    public string Postcode { get; set; }
}
DbContext context = new DbContext("ConnectionString");

var proc = new AddMemberStoredWithAddressesProcedure()
    {
        FirstName = "Michael",
        LastName = "Bovis",
        Age = 26,
        Addresses = new List<Address>()
        {
            new Address() {Line1 = "16", Line2 = "The Lane", Postcode = "MA24WE"}
        }
    };

context.Database.ExecuteStoredProcedure(proc);

Executing a Stored Procedure with an Output parameter

[StoredProcedure("storedProc_GetOldestAge")]
public class GetOldestAgeStoredProcedure
{
    [StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Output)]
    public int Age { get; set; }
}
var proc = new GetOldestAgeStoredProcedure();

context.Database.ExecuteStoredProcedure(proc);

int age = proc.Age; //Is now the oldest age

Useful links

Contribute

The best way to contribute is by spreading the word about the library:

A HUGE THANKS for your help.

More Projects

To view all our free and paid projects, visit our website ZZZ Projects.