ASP.NET 5 , wanted to separate entity framework from Web Project

asp.net-core asp.net-core-mvc entity-framework-core

Question

I'm trying to achieve below application structure

App.Model -- UserModel -- OrderModel

App.Service -- UserService -- IUserService

App.Entity -- DbContext

Website -- WebAPI -- Invokes Service

Most of ASP.NET 5 application examples , initializes entity framework in startup file of website. like .AddEntityFrameowrk() .. etc.

Need to know how we can separate the Entity into separate project and how do we initiate the DBContext if we are invoking repository from Service project.( since startup file is not available for class library)

Popular Answer

In previous projects, I have taken the approach of maintaining multiple ASP.NET projects in one solution (or multiple solutions). This will give you the ability to remove the EF dependencies from your web application and have the web application talk only to the API which handles all the EF

In the past, I've created a dedicated 'domain' project which contains all the models and can be referenced by both your web application and API projects.

So the project structure would look like this

Domain project: Contains only c# classes that represent your model. Start with an 'empty' project

API: Contains your web API. Uses EF to interact with database. I would personally scaffold the APIs fro your model but that is a personal choice. Right now, in ASP.NET core 1.0 RC1, the web api template is poor so I'd start with the full 'web application' one and remove all the default views, controllers, scripts etc. References the domain project so that you can scaffold your API controllers from the model

Web Application: Your main web application project. Start with the 'web application' template but use HttpClient I your controller to use the api for data interactions. In the past I've created a set of static 'api helper classes' to make this task easier but that is a personal style choice again. References the domain project so that you can convert incoming JSON responses to the full .net model



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow