I use identity 2.0 in my MVC 5 project.
First time when I run the project in my DB has been created all default tables for authentication and authorization:
In AspNetUsers table I need to create additional column named LoyoutId of type integer.
My question is how can I add column to created default tables?
You can add profile data for the user by adding more properties to your ApplicationUser class, please visit this to learn more.
In your case:
public class ApplicationUser : IdentityUser
{
public int LoyoutId { get; set; }
}
Then open the Package Manager Console and execute following commands:
PM> Enable-migrations //You don't need this as you have already done it
PM> Add-migration Give_it_a_name //This will generate a database script file
PM> Update-database //Run script file against database.
Now, all the new properties will turn into AspNetUsers table.
Related
New to EF Core 2, used EF 6.x with Database first.
I was hoping that when I scaffolded a database with tables that had pleural names (ie Users) the class created for each table would be singular (ie User). Instead, I get something like public virtual DbSet Users Users { get; set;}. What I was hoping for was public virtual DbSet User Users { get; set;}.
Is there a switch on the Package Manager Console line that will allow me to do this or must I edit after scaffolding?
M
EF Core doesn't include pluralization, but it can be added as a design-time service (IPluralizer).
The EF Core Power Tools include an implementation.
If you're using EF Core 2.1.0-preview2-final, you can use my package:
Install-Package Bricelam.EntityFrameworkCore.Pluralizer -Version 1.0.0-rc2
Scaffold-DbContext ...
I am using EF code-first migrations. I added a new property to my model but it doesn't reflect as a column in the database.
Configuration.cs
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
Global.asax.cs
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, DbMigrationsConfiguration<DbContext>>());
using (var context = new MyDbContext())
{
context.Database.Initialize(true);
}
I have already tried the update-database. It says no pending migrations.
EDIT:
My EF is a separate Class Library Project.
There's another Class Library Project that stores all the Models.
And the Main (Startup) Project is an MVC Project.
Where should I include Migrations?
Also, Add-Migrations Initial always gives me EMPTY Up() and Down() methods.
this worked for me first of all take backup of your database then delete migrations folder in project and delete __MigrationHistory table in database
then run this command
Enable-Migrations -EnableAutomaticMigrations -Force
then add migration
Add-Migration InitialMigration
and finally update database with
Update-Database
Verbose can fix this problem. In your package manager console run
Update-Database –Verbose
Before executing "Update-Database" command did you run "Add-Migrations"?
The Add-Migrations command will generate the necessary migration script to update the database.
Also, do you have a _MigrationsHistory table in your database?
Could you update your code to point to your MyDbContext rather than the base DbContext? And I believe DbMigrationsConfiguration<DbContext>>() should also point to your dbcontext type.
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, DbMigrationsConfiguration<MyDbContext>>())
*Add Your New Model Frst: User Model -id,fname,lname,adress,ect...
*Define in your Application db context the new model as a new set Example :
public DbSet Users{ get; set; }
*Create Migration : add-Migration YourMigrationName
*Update Database
I am trying to develop a small application using ASP.NET MVC 5.
I am using Entity Framework with a code first approach.
To get started, I created my first model and then followed the steps below to create the table in the database
I opened the Package Manager Console
I executed Enable-Migrations
I executed Add-Migration InitilizeModel1Table
Finally, to create the table I executed Database-Update
In step 3 created a migration called datetime_InitilizeModel1Table which created the code that will create the table automatically.
In step 4, it applied the create table command and created the table in the database as expected.
Now, I created 3 more models and what I like to do is create a separate migration for each to keep my code separated.
So I thought I would start again at step 2 and so the following
Add-Migration InitilizeModel2Table
Add-Migration InitilizeModel3Table
Add-Migration InitilizeModel4Table
Database-Update
But the command Add-Migration InitilizeModel2Table is creating an empty migration without the code that is needed to create the tables.
Here is an example of one
namespace App.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class InitilizeModel2Table : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
How can I create a new migration for each model without having to manually write the migration script to create the tables?
The problem here was that you weren't adding your models to the context, hence EF did not detect the changes.
EF links the state of the model to the migration via the __MigrationHistory table/the migration files themselves. It compares the latest value in __MigrationHistory to the current state of your context, if they are different then a new, non-empty migration will be created when you run Add-Migration X.
Here are a couple of resources describing code-first migrations.
I am by no means an EF expert, so I may be slightly off in the details here.
I have searched all over the internet, stack overflow but i am feeling lost in the mystery of Entity framework Migration.
I have existing database. I have added three new tables which have some relation with AspNetUsers table.
I have tried two ways to add these tables to database using pmc.
first comment out all new tables in dbcontext and then use following command.
Add-migration initial -ignorechanges
then uncomment the tables from dbcontext and run update-database command. result no new table added.
then i remove all the entries in migration history table and remove migration folder and start from scratch.
enable-migrations -EnableAutomaticMigrations
Add-migration bcvn
update-database -verbose
now, when i run update database, it says There is already an object named 'AspNetUsers' in the database.Found this article to be helpfull but in my case there are no changes going on AspNetUsers table in up method so i can follow this article http://blog.rajsoftware.com/2014/07/12/entity-framework-code-first-automatic-migration-existing-tables/ Please someone save me.May be i will receive many downvote .but i dont care i want solution.
Got it working. The Trick was to comment out all the code related to new table especially the relationship related code which i was not doing before to ApplicationUser class then
Add-migration initial -ignorechanges
then uncomment all the code and then
update-database -verbose
The only thing which i was not doing before was to comment out the relationship like this--
// public virtual Notification Notifications { get; set; }
and in notification table
// public virtual ApplicationUser ApplicationUser { get; set; }
May be helpful for someone too.
I have an ASP.NET MVC 4 project with Entity Framework 5, .NET 4.5 and Visual Studio 2012.
In my solution I've put all the models in a project called Model, all the Repositories and my DbContext in one more project called Data.
I activate the migrations in the Data project with the Enable-Migrations command. I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well. If, for example, I add a new column to a table, it works fine. I can see the new column in the database schema and I see the new record into the _MigrationHistory table.
At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.
It delete my database, and init it with the initial migration.
I can't tweak a model without loosing all data.
How I can avoid this behavior?
Thanks
UPDATE:
Configuration.cs
namespace NegoziazioneEventi.Data.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<NegoziazioneEventi.Data.NeDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(NegoziazioneEventi.Data.NeDataContext context)
{
}
}
}
Application_Start() in Global.asax
protected void Application_Start()
{
// init basic data on database
System.Data.Entity.Database.SetInitializer(new Models.InitData());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
_container = Bootstrapper.GetWindsorContainer();
}
I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well.
...
At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.
That is completely wrong usage of migrations and it is also the reason why EF deletes your database. You must first add property to model and then add migration because EF needs to store correct data into _MigrationHistory table to match that record with the real meaning of that migration.
With your current approach EF runs the application and checks _MigrationHistory table but the record in the table doesn't contain information about your newly added property so EF believes that new change was done to your model and uses default strategy to delete database and create a new one reflecting your current model. You can turn off this behavior but you should start by using migrations correctly!
To turn off the behavior use:
Database.SetInitializer<YourDatabaseContext>(null);
You are using your own initializer which is most probably derived from wrong build-in initializer causing drop of your current database.