0

I have a web application built with ASP.NET CORE 3.1 (using Razor Pages). I'm using Entity Framework Core (code first) for data access. Everything is in the same project, let's call it MyProject.Web.

Now I have to create a console application that will use the same database. My plan is to add it as a new project in the same solution as the web application: MyProject.Console.

Since the console application will use the same database, it seems to me to be a good idea to create yet another project in the solution, MyProject.Data, that will contain the entities and all code used for database access. That way, both the web application and the console application could use the same code for data access.

Is this a good idea?

I already have some problems… My entities have validation implemented as attributes (from System.ComponentModel.DataAnnotations;). However, I also have created a custom validation attribute, and to apply that to an entity I need to have the validation attribute class in MyProject.Data as well. This validation attribute implements the interface IClientModelValidator so that my custom validation can be applied on the client side as well. The IClientModelValidator interface is found in the Microsoft.AspNetCore.Mvc.ModelBinding.Validation namespace. First of all, I don't know how to add the necessary framework (Microsoft.AspNetCore.App) to the project MyProject.Data. Secondly, I seems a bit weird to add that framework anyway, since this is supposed to be a data access project only.

Any suggestions on how to solve this, or if it even is a good idea, are very welcome.

haagel
  • 2,646
  • 10
  • 36
  • 53
  • Just to add to this, see https://stackoverflow.com/questions/49598884/create-ef-core-as-separate-project-in-visual-studio-2017#comment86207730_49598884 for more details about separating EF Core – dwoodard Apr 22 '21 at 19:40

1 Answers1

2

Yes, its a good idea to seperate the projects (Web App Project, Console App Project and Datebase Project). but exposing domain models (database objects) to UI (Web and Console Apps) is bad idea.

So, in the database project, you will add all your domain models classes with EF/Database annotations (System.ComponentModel.DataAnnotations) only without any UI validations.

In the web application project, you have to add ViewModel/DTO classes, and here you could add the required validations and anything related to UI like combining first name and last name attributes on the domain model to FullName attribute and display it on UI.

You could check here for more about ViewModel:

What is ViewModel in MVC? and https://www.tektutorialshub.com/asp-net-core/asp-net-core-model-and-viewmodel/

Wajdy Essam
  • 4,280
  • 3
  • 28
  • 33