0

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;

            optionsBuilder.UseSqlServer(completedCnnString);
        }

        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();
            base.OnModelCreating(modelBuilder);
        }
    } 
2

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmm, I was very hopeful this would work, but I'm getting a strange NullReference error in the powershell window. System.NullReferenceException: Object reference not set to an instance of an object. at EntFrame.DataAccessLayer.BazaarDBContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\payton.juneau\source\repos\EntFrame\EntFrame.DataAccessLayer\BazaarDBContext.cs:line 17
What does your context look like?
I've updated the question with the code of my context.. The line 17 it is referencing is where I use SqlConnectionStringBuilder with the ConfigurationManager to make connection to my BazaarDB.
I would remove the OnConfiguring method entirely and instead add your connection string to the appsettings.json file of the .NET Core app. ConfigurationManager is used for the classic Web.config files.
Grr, okay I'll try that. I used Vistal Studio to generate the proj so let me figure out how to add and format appsettings.json and I'll update again soon. Thanks for your time an energy, sir. Once this gets solved, I can begin the real project!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.