0) Prepare Database Tables and define relation between tables
1) Install "EF Core Power Tools" from Extensions
2) Create new MVC Project
3) Right click on the solution and choose "EF Core Power Tools" > Reverse Engineering
4) Then choose DB and Tables to generate the related classes and DBContext
- Set EntityTypes Path= Models
- Set DBContext Path= Data
- Check Data Annotation
- Check Include Connection String
- Check Install EF Core Packages
Notes:
After create DBContext, you can use it direct like this
var _db = new NopcommerceContext();
var Lang = _db.Country.ToList();
But it is better to add it to dependency injection using start class ConfigService method.
5) Open \"Startup.cs">"ConfigureServices" add "DBContext" Service
and Update "appsettings.json" add "ConnectionStrings"
Notes:
After define DBContext in dependency injection, we can access DB using any controller like this
public class LanguagesController : Controller
{
private readonly NopcommerceContext _context;
public LanguagesController(NopcommerceContext context)
{
_context = context;
}
}
6) Right click on "Controller" folder and choose "Add new Controller", from the new menu choose "MVC using Entity Framework"
8) Choose class and DBContext and ControllerName then click OK
9) Now you have CURD functionality on the selected tables but you may need to apply one modification if the field is lookup
In Details and Delete update field instead of view Number, it should view the Value
In Create and Edit, it should view drop down
No comments:
Post a Comment