Which is best, entity framework core in a class library, MVC or Blazor server side

iqworks Information Quality Works 276 Reputation points
2023-03-27T16:21:25.0466667+00:00

HI, I am planning to create an MVC project. I wanted to use entity framework core.

I am wondering if I should install it in a class library or an MVC project.

I see that I can use a class library to make the use of EF more available for any current or future projects to access. I can also see that I can use a razor class library so I can have standard pages in a class library. For instance there might be standard page that display EF data in one way that all of my projects can use to display the same data.

I think all of other projects should reference this class library for data. I think I am answering my own question, but wanted other opinions about this.

Thanks for any suggestions, advice but especially your time !

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,425 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2023-03-28T02:32:38.1+00:00

    @iqworks Information Quality Works, Welcome to Microsoft Q&A,

    Which is best, entity framework core in a class library, MVC or Blazor server side

    It depends on your situation, and I have the following advice for this:

    To use EF core in a class library:

    If you want many applications to access your database and do the CRUD opreations, I recommend that you use ef core in a class library.

    To use EF core in a MVC project:

    If you only have one MVC project and want to do some CRUD operations, meanwhile you are familiar with javascript or you want to use javascript to design your UI, I suggest that you could try to use EF Core in a MVC project.

    To use EF core in a Blazor project:

    If you want your app to connect to the server and prefer using c# instead of javascript, I recommend that you could try to use EF Core in a Blazor Server.

    Hope my advice could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 57,401 Reputation points
    2023-04-03T18:37:14.24+00:00

    the data layer should always be its own project. the data layer should not have any ui. any shared UI code associated with the data layer should be its own library. so if you had a Blazor Server and an Razor page project you might have:

    • MyData project - ef code to access the data base
    • MyBlazorServerSharedUI project - shared blazer ui code to access MyData project
    • MyRazorSharedUI project - shared razor ui code to access MyData project

    note: as Blazor WASM could not call EF directly, and if you wanted to share between Blazor Server and WASM, you would need a factory to access the data layer. WASM would need to call a webapi. this is why you would have an access layer between the data layer and any layer using it. it is common for security to require that the UI server can not access the data layer directly (n-tier)