Search code examples
c#.netasp.net-mvcstored-proceduresscaffolding

.Net MVC Database first model with stored procedures


I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.

My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.

Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it? Thanks!


Solution

  • If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.

    If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:

    this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
    

    As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.