I am using Code first model in my asp.net mvc 4 application with DbContext. Currently, I am using simple Add, Update and Delete and Find methods of DbContext class.
But I want to use stored procedures to add, update, delete and find the data so that whenever we do some minor changes in the database. I don’t want to recompile and publish the whole application whenever some minor changes occur in the database. Suppose
we add some column or rename the column and table name, we also need to do changes in the model structure.
Can you tell me the better way to handle it and how to achieve this?
Is using stored procedure with Entity framework code first model is a better way?
If we are using database first model, the same problem arising that we need update the model from database and recompile and publish it?
if you change your database then it usually means that you want to make updates to your application as well. If not then you could go for stored procedures but you'll need to keep the input / output parameters the same as well if you don't want to publish
again as that would mean changing code as well.
You talk about minor changes. Which are those? Changing columns and such? Or laying indexes? If the table and column structures remain the same then you likely could change them without too much hassle, if you start fiddling around with those however then
usually not only that changes and you need to alter code as well.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
I don’t want to update my application. Can you provide some sample or article where better explains: “stored procedures need to keep the input / output parameters the same”
Minor changes such as renaming table or column name.
Please also explain these points:
Can you tell me the better way to handle it and how to achieve this?
Is using stored procedure with Entity framework code first model is a better way?
If we are using database first model, the same problem arising that we need update the model from database and recompile and publish it?
as long as you keep your parameters of your stored procedures the same you don't have to alter your code. It's only the inner workings that change. See it as a function in C# where you change the body of the function but not the parameters of the function
so the code that calls the function doesn't need to be changed. This is the same principle.
If you need to change your tables and columns then you didn't specify them well enough up front while developing so basically change the names before going live. If you make use of Code First also start making use of data migrations which got into EF.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Rohit421991
Member
18 Points
20 Posts
Handle database changes and using stored procedure in entity framework?
Dec 24, 2012 12:48 PM|LINK
I am using Code first model in my asp.net mvc 4 application with DbContext. Currently, I am using simple Add, Update and Delete and Find methods of DbContext class.
But I want to use stored procedures to add, update, delete and find the data so that whenever we do some minor changes in the database. I don’t want to recompile and publish the whole application whenever some minor changes occur in the database. Suppose we add some column or rename the column and table name, we also need to do changes in the model structure.
Can you tell me the better way to handle it and how to achieve this?
Is using stored procedure with Entity framework code first model is a better way?
If we are using database first model, the same problem arising that we need update the model from database and recompile and publish it?
Please reply ASAP.
Thanks.
XIII
All-Star
182684 Points
23455 Posts
ASPInsiders
Moderator
MVP
Re: Handle database changes and using stored procedure in entity framework?
Dec 24, 2012 12:54 PM|LINK
Hi,
if you change your database then it usually means that you want to make updates to your application as well. If not then you could go for stored procedures but you'll need to keep the input / output parameters the same as well if you don't want to publish again as that would mean changing code as well.
You talk about minor changes. Which are those? Changing columns and such? Or laying indexes? If the table and column structures remain the same then you likely could change them without too much hassle, if you start fiddling around with those however then usually not only that changes and you need to alter code as well.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Rohit421991
Member
18 Points
20 Posts
Re: Handle database changes and using stored procedure in entity framework?
Dec 24, 2012 01:55 PM|LINK
Thanks for your soon reply.
I don’t want to update my application. Can you provide some sample or article where better explains: “stored procedures need to keep the input / output parameters the same”
Minor changes such as renaming table or column name.
Please also explain these points:
Can you tell me the better way to handle it and how to achieve this?
Is using stored procedure with Entity framework code first model is a better way?
If we are using database first model, the same problem arising that we need update the model from database and recompile and publish it?
Thnaks in advamce.
XIII
All-Star
182684 Points
23455 Posts
ASPInsiders
Moderator
MVP
Re: Handle database changes and using stored procedure in entity framework?
Dec 24, 2012 02:07 PM|LINK
Hi,
as long as you keep your parameters of your stored procedures the same you don't have to alter your code. It's only the inner workings that change. See it as a function in C# where you change the body of the function but not the parameters of the function so the code that calls the function doesn't need to be changed. This is the same principle.
If you need to change your tables and columns then you didn't specify them well enough up front while developing so basically change the names before going live. If you make use of Code First also start making use of data migrations which got into EF.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Handle database changes and using stored procedure in entity framework?
Dec 25, 2012 12:31 AM|LINK
Hi,
Maybe I think you can just map your CRUD methods to the generated EF's model tables.
For more, you can refer to this:
http://weblogs.asp.net/dotnetstories/archive/2011/03/01/using-stored-procedures-with-entity-framework-in-an-asp-net-application.aspx