290326 (6) [Avatar] Offline
While I await the DBAs granting me CREATE DATABASE so I can use EnsureCreated, I figured I'd run add-migration and update-database from Package Manager Console and continue on.

Unfortunately, though I was able to run Add-Migration Chapter02a -Project DataLayer -StartupProject EfCoreInAction from the PMC, when I tried running Update-Database, I got the following error:

Startup project 'DataLayer' 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.

I've confirmed that the ServiceLayer and DataLayer projects have a target framework of .NET Standard 2.0. If I run Add-Migration Initial against the EfCoreInAction project then, as expected, I get:

No DbContext was found in assembly 'EfCoreInAction'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

Is there a way around this?

(PS: I'm not sure what the [CA] in your Add-Migration example (end of section 2.2) is for, but I wasn't able to run the Add-Migration until I removed it.)
290326 (6) [Avatar] Offline
Oh, I see.

I just found the comments in the code that say to run update-database with the -Project and -StartupProject arguments. I'm pretty sure that detail wasn't in the book, but after I cleared up some other permissioning issues it worked as expected.

Jon P Smith (27) [Avatar] Offline
OK, that makes sense. If you can't create/delete databases then you can't use EnsureCreated. Database migrations will work if there is an empty database there, but you will need to edit the code. This is getting a bit complex because the code is designed to use EnsureCreated. However, you can have a go with the following, but it might not work.

The correct commands to create/add a database migration commands needs
  • -Project to tell it what project the DbContext is in
  • -StartupProject to tell it what the startup project is
    So the commands are
    Add-Migration Chapter02 -Project DataLayer -StartupProject EfCoreInAction
    Update-database -Project DataLayer -StartupProject EfCoreInAction

    The [CA] is Manning code for a return, but it doesn't get converted until its in print

    The problem is that code will set up a database with the name EfCoreInActionDb, while the EfCoreInAction startup code will build a database name which includes the branch name, e.g. EfCoreInActionDb.Chapter02.

    You need to edit the code in the Startup.cs class in the EfCoreInAction project. Simply comment out the part that calls FormDatabaseConnection (lines 42 to 46). The startup code uses EnsureCreated, but if the database is there it won't do anything. The seed should fill the database, as it will add the example books if no books are in the database.

    Good luck!
    290326 (6) [Avatar] Offline
    Yep, I'm all set now. What was confusing me was that EnsureCreated wasn't erroring out, but when I ran a SQL trace I discovered it was in fact trying but failing.

    Thanks for the response. Hopefully this marks the end of my corporate-database-caused issues.