Entity Framework Core Migrations In .NETSTANDARD Class Library?

.net-core .net-standard-2.0 c# ef-core-2.0 entity-framework-core


The main issue: Need to update the SQL DB to have new tables because I added new Entities and DB sets in the DataAccess and Entity layers.

Okay, my project is laid out like so. Solution > DataAccessLayerProject(DB Context), ModelsProject(Entities), TesterProject(Console App).

The console app is a way of running the DataAccess and Entity code.

Here is a pic for reference:

enter image description here

I need Migrations added to the DataAccessLayer, but get the following error:

PM> Add-Migration BazaarDBMigration

Startup project 'EntFrame.DataAccessLayer' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core. PM>

Okay fine, the testing console app references that project. Let's try to add it there..

PM> Add-Migration BazaarDBMigration No DbContext was found in assembly 'EntFrame.TestDriver'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Any ideas? Thanks in advance for any of your time and expertise. :)

EDIT: I tried the solution from the other similar question, but adding a second targetframework pushes the issues even farther down the rabbit hole. I am sorry.

EDIT: Here is the code from my context

public class BazaarDBContext : DbContext
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Sandwhich> Sandwhiches { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            SqlConnectionStringBuilder cnnStringBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["BazaarDBContext"].ConnectionString);
            cnnStringBuilder.UserID = ConfigurationManager.AppSettings["SQLConnectionUser"];
            cnnStringBuilder.Password = ConfigurationManager.AppSettings["SQLConnectionPassword"];

            string completedCnnString = cnnStringBuilder.ConnectionString;


        protected override void OnModelCreating(ModelBuilder modelBuilder)
            //modelBuilder.Entity<Student>().HasKey(s => new { s.StudentID_PK, s.StudentID_PK1 });
            modelBuilder.Entity<Student>().Property(s => s.Email).IsRequired();
8/29/2018 6:54:38 PM

Popular Answer

We do this exact setup with our projects.

The first error is telling you that you need a runtime (which a library project doesn't have). The second error is telling you that you need a DbContext in the library where you're adding the migration. Two different issues, but this should get you on the right track:

  1. Open Powershell and get into the directory of your DataAccessLayer project
  2. Add the migration with dotnet ef migrations add BazaarDBMigration --startup-project ..\EntFrame.TestDriver (assuming the TestDriver project is one level up

This will add the migration to the current project directory and the --startup-project flag specifies where the runtime project is.

8/25/2018 4:50:51 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow