Answered by:
Build failed error!

Question
-
User1267732285 posted
At what level of this project should i be trying to install this migration? I want to install initial migrations to build my database form db context. Was having issues at first about update to ef migrations.. but it is now cleared. But i need to be installing from correct folder in order to make it work! I keep getting build failed error! Cowboy2
I keep getting build failed error! In command Line terminal.
These are the instructions from the project:
Now that the model configuration is complete and provision for seed data has been made, you can create the actual migration. Execute the following command from the terminal:
dotnet ef migrations add CreateDatabase --output-dir Data/Migrations
This creates a migration called CreateDatabase. The files for the migration are generated in a newly created folder named Migrations in the Data folder (as specified by the value passed to the
output-dir
switch): LearnRazorPages.comBut i have installed new migration dictionary as the previous instructions were not working!
And now their is the build failed error.'
I believe that it is saying that i am missing using the correct folder to begin my tooling."
Monday, May 4, 2020 7:57 PM
Answers
-
User303363814 posted
The error tells that you there is no namespace called Data within the namespace called Bakery. This will be because you do not have a code file which declares that namespace. You do have a file called BakeryContext.cs which would define the namespace if it was compiled. Unfortunately, the file is in a sub-folder of the wwwroot folder which is only for static files (css, images and the like). Anything in this folder (or its children) is not compiled. Have a look at the properties of the file.
I suspect that your 'Data' folder should be at a higher level in the folder structure - definitely not within wwwroot. Typically it would be placed in the root of the project. I notice that the previous page of your tutorial says
To add one, create a folder named Data in the root of the project. Then add a C# class file to it named BakeryContext.cs. Amend the content as follows:
So, the tutorial recommends the root of the project as a good place. I would agree with that advice.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 12:44 PM
All replies
-
User303363814 posted
Does your project build? In all configurations? What errors do you get?
Monday, May 4, 2020 11:19 PM -
User1267732285 posted
Paul i tried to run my project in all configurations inside of visual studio. Core 1.1 to Core 3.1 I had no luck. Strange that this is a tutorial and it is recent but the project does not run like its supposed to!
Steps: Create the Migration at bottom of Page."
https://www.learnrazorpages.com/razor-pages/tutorial/bakery/migration
LearnRazorPages.com/2020
Last updated: 29/05/2019 08:05:08
Tuesday, May 5, 2020 5:21 PM -
User303363814 posted
Don't worry about migration for the moment. It seems that the problem is your project has compilation errors somewhere. You need to be able to build the project(s) before you can make any progress with things like migration.
Go to the 'Build' menu and choose 'Build Solution'. Do you get errors? What are they? There may be more than one error, only focus on the first one. What is the exact error message? Does it refer to a line of your code? Which line? Show us the line of code with the error (and the method it is in if the method is not too long).
(If 'Build Solution' doesn't give errors then do a 'Build'=>'Clean Solution' and when it finishes try the 'Build Solution' again)
Wednesday, May 6, 2020 12:51 AM -
User1267732285 posted
Paul, you were correct about the build. I found the issues and i was missing only some curly brackets in two of the classes. And now here are the most current errors since building the project. The project is saying the class is not present but i can definitely see it there." Paul as per the instruction for the project, it says to add a using reference for Bakery Data. When i add this reference it throws an error unexpectedly. And then it becomes two using reference errors? Cowboy2
ps: see the "Adding and registering a Context" page 2. portion of the project tutorial. (add the using reference for Bakery Data)
Friday, May 8, 2020 12:03 AM -
User288213138 posted
Hi Cowboy2,
Is your dbcontext like below code?
namespace Bakery.Data { public class BakeryContext : DbContext { public BakeryContext (DbContextOptions<BakeryContext > options) : base(options) { } public DbSet<Table> Tables{ get; set; } } }
If so, your Startup.cs should look like this.
services.AddDbContext<Bakery.Data.BakeryContext >(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));
If your Startup.cs is this.
services.AddDbContext<Bakery.Data.BakeryContext >();
Then you need to add below code in your DbContext.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=YourDatabase;Trusted_Connection=True;"); } }
When i add this reference it throws an error unexpectedly. And then it becomes two using reference errors?Please post your detailed error message. Can you show me your complete dbcontext?
Best regards,
Sam
Friday, May 8, 2020 7:35 AM -
User303363814 posted
Right-click on the red squiggly line. Choose "Quick actions and refactorings" The first option is probably "Using something". Select it. Does the red squiggly go away?
Friday, May 8, 2020 12:03 PM -
Friday, May 8, 2020 3:41 PM
-
User1267732285 posted
Paul, sorry but the quick action and refactorings does not fix errors!
Friday, May 8, 2020 3:43 PM -
Friday, May 8, 2020 3:48 PM
-
User303363814 posted
The error tells that you there is no namespace called Data within the namespace called Bakery. This will be because you do not have a code file which declares that namespace. You do have a file called BakeryContext.cs which would define the namespace if it was compiled. Unfortunately, the file is in a sub-folder of the wwwroot folder which is only for static files (css, images and the like). Anything in this folder (or its children) is not compiled. Have a look at the properties of the file.
I suspect that your 'Data' folder should be at a higher level in the folder structure - definitely not within wwwroot. Typically it would be placed in the root of the project. I notice that the previous page of your tutorial says
To add one, create a folder named Data in the root of the project. Then add a C# class file to it named BakeryContext.cs. Amend the content as follows:
So, the tutorial recommends the root of the project as a good place. I would agree with that advice.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 12:44 PM