EntityFrameworkExtras provides some useful additions to EntityFramework such as executing Stored Procedures with User-Defined Table Types and Output Parameters.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
EntityFrameworkExtras.EF5.Tests - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.EF5 - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.EF6.Tests - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.EF6 - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.Tests - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras updated assembly version too Apr 4, 2016
packages - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
.gitignore - .gitignore Nov 24, 2012
CHANGELOG.md - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.sln - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
EntityFrameworkExtras.vsmdi
Local.testsettings - added version 1 of EntityFrameworkExtras project Nov 24, 2012
README.md renamed stored procs Mar 17, 2016
TraceAndTestImpact.testsettings - added version 1 of EntityFrameworkExtras project Nov 24, 2012
icon.png - A major overhaul of the structure of the project. The changes are t… Jun 24, 2014
licence.txt Rename licence to licence.txt Jun 26, 2014

README.md

EntityFrameworkExtras

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

Nuget

###Executing a Stored Procedure with a User Defined Table Type###

  • Define a stored procedure class
[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; }
}
  • A User Defined Table Type parameter is declared as a List<> (List). The UDT will also require some attributes:
[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; }
}
  • Execute the Stored Procedure with either a DbContext or an ObjectContext
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 a Output parameter###

  • To add an Output parameter you just need to set the Direction parameter to ParameterDirection.Output.
[StoredProcedure("storedProc_GetOldestAge")]
public class GetOldestAgeStoredProcedure
{
	[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Output)]
	public int Age { get; set; }
}
  • Execute the Stored Procedure and the parameter will be set to the output parameter value
var proc = new GetOldestAgeStoredProcedure();

context.Database.ExecuteStoredProcedure(proc);

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

###Thanks to the contributors!###