Required field requires migration? - c#

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

Related

Add-Migration not working in asp.net core

I'm tried to Add-Migration in my asp.net core application but migration not working
Please any one give me ans for this what's the issue
Please check this image i added all information reading issue everything i combined into single screen-shot for more clarification
you need to provide a name for the migration just like this Add-Migration V1, after creating the migration you need to run the Update-Database to apply the migration. BTW if there is any build error nothing will work. you need to ensure there is not build error.
I would do this:
1) Remove the Migrations folder in your project
2) Delete the _MigrationHistory table from your database.
3) Run the Enable-Migrations command from your Package Manager Console.
4) Run the Add-Migration Init command to create a migration.
5) Remove all of the code lines in Up() function for the initial migration. (To create an empty migration.)
6) Run the Update-Database command to apply your empty migration.
7) Make changes to your code first model for preparing to adding your real migration.
8) Run the Add-Migration InitNew command to create a new migration.
9)Run the Update-Database command to apply the changes.
Taken from: https://stackoverflow.com/a/44019743/6221069

.NET Entity Framework Update-Database default startupprojectname

Every time I want to update database with code-first migrations I need to open Package Manager Console, set up Default project to project where I have migrations and write command:
update-database -startupprojectname CoreConsole
How I can set up in my solution:
1) Default project in dropdown where migrations are stored
2) startupprojectname where config with connection settings is stored
The target is to only write command update-database in console without any parameters and additional dropdown selections.

Where is the -ConfigurationTypeName located?

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

I cannot login after deployment

I've published my ASP application on my local machin using IIS10.
when I try to login or register a new user after deployment I got this error
Unable to update database to match the current model because there are >pending changes and automatic migration is disabled. Either write the pending >model changes to a code-based migration or enable automatic migration. Set >DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable >automatic migration.
I use visual studio 2013, Entityframework 6.1.3,Windows Authentication
You need to run Update-Database against your target database.
Open the package manager console window and select your Entity Framework project, then type that command. As long as the config is pointing to the correct DB, it should update it.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
PM>

EF 6 CodeFirst Migrations Not Working

Ok, so I've read the other threads and I'm still not getting anywhere. I have my project set as Startup, I have a connection string in the App.config. I'm using LocalDb in VS2013.
If I delete my database, it creates it ok, BUT, if I try
PM> Enable-Migrations
it tells me that Migrations have already been enabled in project TestCodeFirst. If I then try
PM> Update-Database
it tells me No migrations configuration type was found in the assembly 'TestCodeFirst', and suggests that I try to Enable-Migrations. I have tried adding the -Force parameter. The Migrations folder has only Configuration.cs, and in that file, I've tried setting AutomaticMigrationsEnabled both true and false, with no difference. I'm not lost, cause I can always delete my DB and rerun, but I don't see any way to make the migrations feature work as advertised. Confession: At one point, I took PM's suggestion and tried deleting the Migrations folder. That may have been a mistake.
I have this code snippet in my Program.cs file (I'm just testing this in a console app):
class Program
{
static void Main(string[] args)
{
using (TestContext db = new TestContext())
{
...
Db.ThisOrThat.Add(stuff);
Db.SaveChanges();
...
}
}
}
The Entity Framework Code First approach creates your database automatically, a table will be automatically added to the database by the Code First that is helpful to record the schema of database synchronization with the related model class. Entity Framework throws an error, the database is not synchronized with the class.
To perform the Code First Migration in the database:
Delete the MDF file of the database
Open Package Manager Console and write the following command
Enable-Migrations -ContextTypeName ConsoleApp.Models.TestContext
It creates the Configurations.csclass, then you can edit your code
After Building the application enter the following command in the Package Manager Console:
add-migration Initial
update-database
Note: If you receive an error then you need to ensure that the mdf file is deleted permanently from the App_Data folder. Otherwise you can go to the SQL Server Object Explorer, expand the localDb server and in the Database option ensure that the mdf file is deleted.
Debug the appliction
You'll need to run the "Add-Migration" command that will actually add the migration that the 'Update-Database' command will run:
Code First Migrations
"Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created"

Categories