Note: This relates to Entity Framework Core Only.

By default, Entity Framework Core (EF) assumes it resides within the start-up assembly.

This can be a problem because we would like to have a separation of concerns. Generally, the start-up assembly is concerned with providing the UI or API and not data access. Our Repository Assembly should handle that and be referenced by the start-up (Views) assembly, via interface classes, which play the role of contract between Views and Repository.

Page-1 Sheet.18 Sheet.20 «subsystem» Data.Persistence «subsystem» Data.Persistence Sheet.21 Dynamic connector.222 Dynamic connector.223 Sheet.316 Sheet.309 Sheet.311 Sheet.312 Sheet.487 Sheet.488 Sheet.489 Sheet.490 Sheet.554 Sheet.555 Dynamic connector.446 Sheet.557 Dynamic connector.558 Sheet.576 :Entity Framework Core :Entity Framework Core Sheet.577 Sheet.578 Sheet.579 Sheet.580 Sheet.586 Sheet.29 «subsystem» Data.Core «subsystem» Data.Core Sheet.31 :IRepositories :IRepositories Sheet.33 :IUnitOfWork :IUnitOfWork Sheet.115 Domain Domain Sheet.120 Sheet.329 Sheet.330 Sheet.331 Sheet.332 Sheet.333 Sheet.334 Sheet.335 Sheet.336 Sheet.337 Sheet.338 Sheet.339 Sheet.340 Sheet.341 Sheet.342 Sheet.343 Sheet.344 Sheet.27 Sheet.30 Sheet.542 Sheet.587 Sheet.235 «subsystem» Views «subsystem» Views Sheet.236 Sheet.237 Sheet.361 Sheet.362 Sheet.363 Sheet.364 Sheet.588 Sheet.575 :IRepositories :IRepositories Sheet.581 Sheet.582 Sheet.583 Sheet.584 Sheet.585 Sheet.589 :IUnitOfWork :IUnitOfWork Sheet.590 Sheet.591 Sheet.592 Sheet.593 Sheet.543 Sheet.138 Sheet.139 Sheet.140 Sheet.544 Sheet.545 Sheet.546 Sheet.547 Sheet.548 Sheet.451 Dynamic connector.452 Sheet.453 Sheet.553 Sheet.445 Dynamic connector.446 Sheet.447
Data.Persistence (Repository), Data.Core (Interface contract) and Views

By default, performing database migrations will not work when EF Core is within an assembly other than the start-up assembly. Carry out the following to fix this:

  1. Three packages should be imported into the Repository Assembly, using NuGet Package Manager:

    • Microsoft.EntityFrameworkCore.Design
    • Microsoft.EntityFrameworkCore.SqlServer
    • Microsoft.EntityFrameworkCore.Tools
  2. Once installed edit the Repository Assembly .csproj file

    • Add the following line to a <PropertyGroup>.
      <GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
    • Remove <PrivateAssets>all</PrivateAssets> from elements <PackageReference Include="Microsoft.EntityFrameworkCore.Design"> and <PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
  3. Add the override method OnConfiguring to your class that inherits DbContext.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(SampleDbContext.GetConnectionString());
    }
    
  4. Re-build the application
  5. You will now be able to use EF Package Manager Console commands pointing to the Repository Assembly. For example:

    add-migration InitialModel -Project MyAppNamespace.Data.Persistence
    Create a database migration cs code
    update-database -Project MyAppNamespace.Data.Persistence
    Update the database using the database migration cs code
Copyright © 2024 delaney. All rights reserved.