Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Update DBContext Class in Entity Framework Core

I am using Entity Framework Core in my project , Created a DbContext class using the command

PM> Scaffold-DbContext "Server=servername;Database=DBname;User Id=user;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context QuestionnaireEntities -Tables Category_Master,Item,Option_Master,Question_Master,Sub_Category_Master,Sub_Item

It is Creating DBContext Class perfectly , But if i want to update my DBContext from database than how i can do it in EFcore , In EF its easy just by doing right click and selecting option update from Database

Additionally how can i use stored procedure in my DBContext class

on msdn website they mentioned a command

PM> get-help scaffold-dbcontext –detailed

But it does not have any info about updating DBContext and adding stored procedure in it

How can i do that in EF Core I am using VS 2017 Community edition with .Net core 2.0 and EFcore 2.0

like image 752
Saurabh Avatar asked Jun 28 '26 19:06

Saurabh


2 Answers

You can re-scaffold with -f/--force flag and this will overwrite existing files.

like image 64
Jaliya Udagedara Avatar answered Jun 30 '26 12:06

Jaliya Udagedara


This is simple query just run this in console here you can mention dbcontext if 
you have multiple dbcontext files.  
Scaffold-DbContext "Server=servername;Database=dbname; 
User ID=userid;Password=password; MultipleActiveResultSets=true;" 
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -context 
DbContext 
like image 29
Saeed Ahmad Avatar answered Jun 30 '26 12:06

Saeed Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!