Where is the -ConfigurationTypeName located? - c#

I'm trying to run a add-migration via my .net MVC project. There are two DBContext in a DBContext folder. However, when I run the command:
add-migration -ConfigurationTypeName xxxxx.Configuration -Name SavedSearches
it tells me:
The migrations configuration type 'xxxxx.Configuration' was not be found in the assembly 'xxxxx'.
Where do I find the proper name/namespace for the configuration type (i.e. what folder is it in, what level)? I've tried to search, but nothing clearly states it out.
Thank you.

From the Tools menu, click Library Package Manager and then Package Manager Console.
At the PM> prompt enter the following commands:
enable-migrations
add-migration InitialCreate
update-database

Related

How to enable-migration well without conflict other migration?

I want to update-database, but i see i first need to enable-migration first to the target database column
When i use this command on my console
Enable-Migrations
More than one context type was found in the assembly 'eNtsaRegistrationTraining'.
To enable migrations for 'eNtsaRegistrationTraining.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName eNtsaRegistrationTraining.Models.ApplicationDbContext.
To enable migrations for 'eNtsaRegistrationTraining.DAL.eNtsaRegCourses', use Enable-Migrations -ContextTypeName eNtsaRegistrationTraining.DAL.eNtsaRegCourses.
To enable migrations for 'eNtsaRegistrationTraining.DAL.eNtsaRegistration', use Enable-Migrations -ContextTypeName eNtsaRegistrationTraining.DAL.eNtsaRegistration.
PM> Enable-Migrations -ContextTypeName eNtsaRegistrationTraining.DAL.eNtsaCourses
The context type 'eNtsaRegistrationTraining.DAL.eNtsaCourses' was not found in the assembly 'eNtsaRegistrationTraining'.
I only need eNtsaCourses one, how can i resolve this issue with command console?
Enable-Migrations -ContextTypeName eNtsaRegistrationTraining.DAL.eNtsaRegCourses -force

How to re-migrate already Migrated dbcontext in EF

I have a project call RemoteWork where i have my Dbcontext. I have 2 models inside the DbContext - USER and PRODUCTS. I was using code first approach. I then used the "add-migration" and it was successful. I then referenced the Remotework in another project call Apiclient. They are both in the same solution. Now I have altered my table in USER and so it was out of sync. I wanted to make it to be in sync. I have tried different methods i have read online, I was getting different error messages. Can anyone please help in this regard.
I have done this: Add-Migration SecondMigration
This is the error message:
No DbContext was found in assembly 'AppClient'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
Please note that AppClient was made as the startup project. I have also tried making remotework as a start up project, it did not resolve it either:
PM> Add-Migration SecondMigration
No DbContext was found in assembly 'AppClient'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
PM> Add-Migration -Name MyMigration -OutputDir MyMigrationDir -Context BettingDbContext -Project RemoteWork -StartupProject AppClient
I wanted the models to be in sync with my database
I assume you are running add-migration from the package manager console in visual studio.
Either make sure you have selected the project that contains your context in the "Default project" dropdown at the top of the package manager console. Or use the -StartUpProjectName argument on add-migration.

Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. (Migrations)

I have a project with this structure
TooSeeWeb.Infrastructure is for migrations.
When I try to run migrations with this command
dotnet ef migrations add ExampleMigration -s ..\TooSeeWeb
I have this error
Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option
How I can fix this?
This is 2 years old but I was just in the same situation so it is still relevant. It's first result on Google for this error.
So I can see in your Screenshot that you are not in the standard Windows Visual Studio so I assume you are not on Windows (makes a difference in how to write file paths). Also I can see that you used ..\TooSeeWeb with a backslash.
Solution: Change all \ to a / forward slash so in your case I guess it would be:
dotnet ef migrations add ExampleMigration -s ../TooSeeWeb
For me it was working on Windows but failing on macOS (OS X) with this error:
Unable to retrieve project metadata. Ensure it's an SDK-style project.
If you're using a custom BaseIntermediateOutputPath or
MSBuildProjectExtensionsPath values, Use the
--msbuildprojectextensionspath option.
Additionally it gives the information (that gives a better hint):
MSBUILD : error MSB1009: Project file does not exist.
Here my more complex statement WORKING with forward slashes:
dotnet ef --startup-project ./MainProject.csproj migrations add MyMigration --context MyDbContextPostgreSQL --output-dir Migrations --project ../MyDatabasePostgreSQL/MyDatabasePostgreSQL.csproj
You have to point to your web project
dotnet ef --startup-project ../TooSeeWeb migrations add MigrationName -c NameOfYourDBContext
More details about multi project you can find https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects
I had the same error. I solved it by including the nuget package:
Microsoft.EntityFrameworkCore.Tools
just add the path to your startup project and database project like this:
dotnet ef database update --verbose --project "src/Services/Discount/Discount.Infrastructure.Rdms" --startup-project "src/Services/Discount/Discount.Web"
The path should be relative to your current directory to the destination directory, for instance, if I change my current directory to /src the path would be Services/Discount/Discount.Infrastructure.Rdms
The problem for me was that the project was in the solution folder so I had to specify the project path like this
dotnet ef migrations add InitialCreate --project SolutionName/ProjectName.csproj
My error was similar, but different. In fact the same error as #CodingYourLife mentions (perhaps the error wording has changed over time, or it is in fact a different error), anyway... This was the error:
Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
It turned out that I needed to run the dotnet ef dbcontext scaffold <list_of_options> command from the parent folder (the one with the solution file in it - not from the PROJECT folder). I used cd .. (to move up one folder) and re-ran the command, and it created my database-context and EF classes for the tables specified.
I don't know how this problems appeared.
But i truly know that most developers come from using these commands:
dotnet ef migrations add "MigrationName" -s ../ProjectName
dotnet ef migrations remove -s ../ProjectName
dotnet ef database update -s ../ProjectName
So replaced them with these:
dotnet ef --startup-project ../ProjectName migrations add
MigrationName -c DbContextName
dotnet ef --startup-project ../ProjectName database update -c
FoodTownDbContext
I think the same apply for this,but haven't tested it:
dotnet ef --startup-project ../ProjectName migrations remove
-c DbContextName
first of all, your need to see the exact context name. This is needed for
dotnet ef migrations add -c <your context name>
You can see your contexts in your project by using this command:
dotnet ef dbcontext list
You will see something like below👇
AspNetCore.Jwt.Sample.Config.MyIntIdentityContext
AspNetCore.Jwt.Sample.Config.MyIdentityContext
See... there is more than one context. Choose&copy your context's full name and paste it after -c option
dotnet ef migrations add Initial -c AspNetCore.Jwt.Sample.Config.MyIntIdentityContext
use as above☝.
and that is ok! It works well now.
possible reasons:
main reason is your project includes more than one context class
in my case. Context class was in a file with other classes.(i.e.) this config (.cs)file from this impressive github repository
usually I have this problem when I receive updates for dotnet sdks. ProgramFiles/dotnet/sdk.
The problem is that I use an MSBuildSDKsPath system variable and this path doesn't update after dotnet sdk updates.
If you face the same problem, and use the same system variable, that can be the solution.
I am using Entity Framework Core .NET Command-line Tools 6.0.10, and the following worked for me.
Make sure you install the EF tooling globally
dotnet tool install dotnet-ef --global
Make sure you update it to the latest
dotnet tool update dotnet-ef --global
Open a Powershell off your start up project. In your case, it would be TooSeeWeb? Your Powershell should default the root to something like
PS C:\your-user\source\repos\TooSeeWeb\src\TooSeeWeb>
Run the following command to add a migration
dotnet ef migrations add ExampleMigration
-c YourDbContext
-p ../TooSeeWeb.Infrastructure
-o Data/Migrations
-p should set the project where you want the migrations to be put in.
-o is the directory you want to put files in, and it's relative to the project directory you just set with -p.
You can also use -s to set the start up project, where you have your connection string. But since I open the Powershell off the start up, I don't have to specify this, as it defaults to the current working directory.
You can do dotnet ef migrations add -h to see the whole list of optional arguments and what they do.
In my case it was caused by EF versions that I had installed.
I had Entity Framework Core .NET Command-line Tools 7.0.1 installed and the project I was trying to add the migration to, was using EF 6.
Solution was to run EntityFramework6\Add-Migration instead of Add-Migration

ASP.NET Core EF Add-Migration command not working

Following this Microsoft Tutorial when I run the PM> Add-Migration MyFirstMigration command in VS2015 project created from the same tutorial I get the following error that I can't resolve:
More than one DbContext was found. Specify which one to use.
Use the '-Context' parameter for PowerShell commands and the
'--context' parameter for dotnet commands.
Point to note
I'm using the above tutorial with the exception that I'm using Individual User Account authentication instead of No Authentication used in the tutorial.
I've latest release of ASP.NeT Core 1.0 and VS2015-Update 3 on windows 8.1
This is a freshly created project. No other DbContext was manually installed
Running the following command (obtained from this article) and a response from #Maverik (from StackOverflow here) and a suggestion from #doctor above helped me resolved the issue. Thank you all for your help:
PM> Add-Migration MyFirstMigration -Context BloggingContext
The error clearly explains to mention --context with db Context name if more than one DbContext. So try by mentioning your DbContext name.
dotnet ef migrations add Initial --context SampleDbContext
Hope this helps.
If you need only update a identity schema existent, try it:
update-database -Context ApplicationDbContext
ApplicationDbContext = your identity context
that because you have two DbContext in your solution. First is default created when you creating project(ApplicationDbContext) and second your EF DbContext.
Solution is described in error message just specify your EF DbContext
Add-Migration MyFirstMigration -Context DbContextName
It does work in my project.
Use below to commands:
PM> Add-Migration MyFirstMigration -Context YourDbContext
PM> update-database -Context YourDbContext
[--context]
The DbContext class to use. Class name only or fully qualified with
namespaces. If this option is omitted, EF Core will find the context
class. If there are multiple context classes, this option is required.
Add-Migration MyMigration -context DataContextName

Required field requires migration?

I removed the data annotation Required from a field. Do I need to use migrations to apply that in the DB? If yes, how can I do that?
To add a migration you go to the Package Manager Console
Run command -> Enable-Migrations
Run command -> add-migration [migration_name]
Run command -> update-database
To apply on production database you can create a script in the following manner
Update-Database -Script -SourceMigration:[NameOfMigrationYourDatabaseIsRunning]
run
update-database -verbose
in the package manager console

Categories