Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core MVC - Can I move Identity and DataAccess to class library

I have created an ASP.NET Core MVC Web application with Individual User Account authentication (Identity).

The template has created one Web project, with a whole bunch of folders, including a "Data" folder which has the migrations for the Identity schema, and ApplicationDbContext.

Now, I have some other projects alongside the web app which will need to consume the data. I don't want them to reference the web project for obvious reasons.

And ideally I don't want my web project to depend directly on EF.

Can I move the data access into a separate class library? And if so, how!?

like image 994
Paul Avatar asked Oct 19 '16 21:10

Paul


2 Answers

  1. Create a class library projects.
  2. Move the content from corewebproject/data to class library projects.
  3. Add following from nuget:

    • Entity Framework
    • AspNetCore.Identity
    • AspNetCore.Identity.EntityFramework
    • Microsoft.entityframeworkcore.SqlServer
    • Microsoft.entityframeworkcore.Tools
    • Microsoft.entityframeworkcore.Tools.Dotnet
  4. Build class library projects.

  5. Add as reference to your web project.
  6. Change reference in startup contextdb file location.
  7. If you want to change you sql server from localdb change defaultconnection in appsettings.
  8. Add reference related files.
  9. Build solutions.
  10. Go to nuget package manager console and select your project.
  11. Run next commands:

    • 'Remove-Migration'. it will remove some file including snapmodel file

    • Add-Migrations "Name"

    • update database

  12. Check you database: you can see upadated db with aspnetcore individual account related tables.

!!! Enjoy !!!!

like image 182
Masrooj Avatar answered Sep 25 '22 00:09

Masrooj


Sure, check out the Dev branch on https://github.com/MachUpskillingFY17/JabbR-Core we just moved all data into a separate library including identity. Its still quite a work in progress, but it absolutely works.

like image 39
Adam Tuliper Avatar answered Sep 25 '22 00:09

Adam Tuliper