Mvc4 uses Entity Framework to map the database to the entity model. The database mapping is done automatically by adding a new ADO.NET component to the project. The following picture shows scaffolded files holding POCO classes and the context. "TestModel.Context.cs" holds the database context derived from DbContext class. "TestModel.tt" holds the model files containing the POCO classes.
Image 1: Entity data model used for testing |
To refresh the database, go to View -> Other Windows -> Entity Data Model Browser, find the desired model in the Model Browser, right-click and select "Update Model from Database". There you can edit the parts of the database the model encompasses. This can be used to add the newly created stored procedures to the model.
Image 2: Refreshing the model to include newly made stored procedure |
Image 4: Stored procedure import window |
Entities _context = new Entities();
var data = _context.MyStoredProcedure("Author name");
Scaffolded stored procedure call:
public virtual ObjectResult<Author> MyStoredProcedure(string authorName)
{
var authorNameParameter = authorName != null ?
new ObjectParameter("AuthorName", authorName) :
new ObjectParameter("AuthorName", typeof(string));
return ((IObjectContextAdapter)this)
.ObjectContext
.ExecuteFunction<Author>("MyStoredProcedure", authorNameParameter);
}
Sources and further reading:
Nice blog, thanks for sharing this information via screenshot and i bookmark this blog for future use.
OdgovoriIzbriši.NET MVC Development