0

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

So what'd be the syntax in my example? Just copy paste doesn't work and the "more info" doesn't help me in the first reading
After this Package Manager > Enable-Migrations -EnableAutomaticMigrations execute PM > Add-Migration. It will ask you for Name. This will create the actual migration (and you will see the file in the Migrations folder). And then this PM > update-database (-f)
So I believe I found the solution. For some reason EF thought the document table already existed, which didn't, so I just removed it from the code and made the whole migration-stuff again (now non-empty migration dropping the documents table), then readd it and now it works
I'm glad that you found the solution (and hope that I've helped)
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.