locked
Target Project does not match your migration assembly.Please help RRS feed

  • Question

  • User-1355965324 posted

    In  my project , I am keeping DbContext  in a separate project from the  web app project. This is my program structure is given below. But when I give Add-Migration the following error is coming

    Your target project 'LibraryBook' doesn't match your migrations assembly 'LibraryBook.DataAccess'. Either change your target project or change your migrations assembly.
    Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("LibraryBook")). By default, the migrations assembly is the assembly containing the DbContext.
    Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

    LibraryBook[WebApplication]
                       -> Views
                      ->Appsetting.json
                     ->Startup.cs
                      ->program.cs
    
    
    
    LibraryBook.DataAccess [ another project]
                Data [folder]  -> ApplicationDbContext.cs
    
    
     

    Appsetting.Jason

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=LibraryBooks;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },

    DbConext.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using LibraryBook.Models;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    
    namespace LibraryBook.DataAccess.Data
    {
        public class ApplicationDbContext : IdentityDbContext
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }

    Saturday, May 2, 2020 8:02 AM

Answers

  • User475983607 posted

    Thanks mgebhred

    I did the ways you advised

    1. Made the startup project LibraryBook.Dataccess
    2. Then After giving the migration through Package Manage console it shows the error 

    Could not load assembly 'LibraryBooks'. Ensure it is referenced by the startup project 'LibraryBooks.DataAccess'.

    LibraryBooks is the web project and I already given the reference to DataAccess project . When I given the reference from DataAccess to LibraryBooks it becomes circular reference

    You did not follow the instructions!   The web application is the startup project not the data access project.  The web application has the configuration file so it needs to start.

    Then in the PMC window select LibraryBooks.DataAccess as the default project.

    If you are having trouble with understanding Visual Studio then use a CLI command which allow you to specify these values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2020 2:05 PM

All replies

  • User475983607 posted

    Set the web application as the startup project.  Right click and select "Set as Startup project".  I assume you are using the Package Manager Console.  Just change the default project to the DbContext project.  Then execute the add-migration.   

    You can also set these value in the migration command.  The openly published CLI documentation has all the information you need to create the command.  Please Read the docs...

    Saturday, May 2, 2020 10:45 AM
  • User-1355965324 posted

    Thanks mgebhred

    I did the ways you advised

    1. Made the startup project LibraryBook.Dataccess
    2. Then After giving the migration through Package Manage console it shows the error 

    Could not load assembly 'LibraryBooks'. Ensure it is referenced by the startup project 'LibraryBooks.DataAccess'.

    LibraryBooks is the web project and I already given the reference to DataAccess project . When I given the reference from DataAccess to LibraryBooks it becomes circular reference

    Saturday, May 2, 2020 1:37 PM
  • User475983607 posted

    Thanks mgebhred

    I did the ways you advised

    1. Made the startup project LibraryBook.Dataccess
    2. Then After giving the migration through Package Manage console it shows the error 

    Could not load assembly 'LibraryBooks'. Ensure it is referenced by the startup project 'LibraryBooks.DataAccess'.

    LibraryBooks is the web project and I already given the reference to DataAccess project . When I given the reference from DataAccess to LibraryBooks it becomes circular reference

    You did not follow the instructions!   The web application is the startup project not the data access project.  The web application has the configuration file so it needs to start.

    Then in the PMC window select LibraryBooks.DataAccess as the default project.

    If you are having trouble with understanding Visual Studio then use a CLI command which allow you to specify these values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2020 2:05 PM
  • User-1355965324 posted

    Many Thanks

    Saturday, May 2, 2020 2:47 PM