rst
rst

Reputation: 2724

Entity Framework doesn't add new table to database

Usually I make projects with database first, now I tried (due to the ASP.NET login framework) the code first approach. So far so good, all works. Now I needed to add a new model to the project, did this and added the model in the DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<Order> Orders { get; set; } // existing one
    public System.Data.Entity.DbSet<Document> Documents { get; set; } // new one
}

So I already have data in the orders table in my database and users in the asp.net user tables, I can't allow to drop the db, so I thought, automatic migration:

Package Manager > Enable-Migrations -EnableAutomaticMigrations

And then

PM > update-database (-f)

however, this returns:

No pending explicit migrations.

The table just won't be created. However, a new folder called Migrations was created with 2 files: ###########_dbname.cs and Configurations.cs

Any hints why my table won't be created? I even tried this in the migration file

public partial class project: DbMigration
{
    public override void Up()
    {
        CreateTable("dbo.Documents", c => new
        {
            DocumentId = c.Int(nullable: false, identity: true),
            Label = c.Int(nullable: false),
            Data = c.Binary(),
            OrderId = c.Int(nullable: false)
        }).PrimaryKey(t => t.DocumentId)
        .ForeignKey("dbo.Orders", t => t.OrderId, cascadeDelete: true);

in vain

UPDATE

If I perform add-migration, the following file is being created, how do I modify it?

using System;
using System.Data.Entity.Migrations;

public partial class documents : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

simply performing update-database doesn't change anything

Upvotes: 0

Views: 2272

Answers (3)

Majesty Cherry Tomato
Majesty Cherry Tomato

Reputation: 181

I realized I had the same problems, I run add-migration rewreww, the migration script does not generate tables, then I realized you need to delete whatever is in the snapshot, then rerun it solved my problem. thanks m3n7alsnak3

Upvotes: 0

m3n7alsnak3
m3n7alsnak3

Reputation: 3166

You need to add the migration for your changes, and then execute the Update-Database. The fact that the Migrations folder has only 2 files, and none of them is a migration file(TimeStamp_MigrationName.cs), tells you that there is no migration to be executed. You need something like (after enabling migrations):

PM > Add-Migration -Name NewEntityAdded

The entire syntax is:

Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] []

And after that - execute Update-Database.

More info - Here

EDIT after the update

The fact that the migration file is empty, means that when executing add-migration there was no difference between the current snapshot and the one that you are trying to migrate to. You don't have to modify the migration file. All the changes must be there. According to the file, there is nothing to change. This is why 'update-database' is not changing anything

Are you still keeping this:

public partial class project: DbMigration
{
public override void Up()
{
    CreateTable("dbo.Documents", c => new
    {
        DocumentId = c.Int(nullable: false, identity: true),
        Label = c.Int(nullable: false),
        Data = c.Binary(),
        OrderId = c.Int(nullable: false)
    }).PrimaryKey(t => t.DocumentId)
    .ForeignKey("dbo.Orders", t => t.OrderId, cascadeDelete: true);

in your code? If yes - remove it

Upvotes: 3

Sreekanth Jagaleti
Sreekanth Jagaleti

Reputation: 59

Please try the following steps:
1. Run DbContext.tt file after creating the model class
2. In Package Manager Console, select your migrations project (if you have created one) as a default project
3. Run add-migration -startupProject "your startup projectname" [Migration_Name]
4. Run DbContext.Views.tt file
5. update-database -startupProject "your startup projectname" -Verbose

Upvotes: 0

Related Questions