0

I am using entity framework to attach to an existing database where I will add a few more tables. Someone on here said this is not possible and I would need to keep the new tables separate in a new database. Here is that question:

Do not create existing table during Migration

I did some more investigation and found this on MSDN:

https://msdn.microsoft.com/en-us/data/dn579398.aspx

According to this I should run an initial migration like this:

add-migration initial -ignorechanges

so I did that and that is supposed to look at the database and match it up. After I update the database, then I am supposed to add another migration without the -ignorechanges. When I do the second migration, I get this:

namespace PTEManager.Domain.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

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

        public override void Down()
        {
        }
    }
}

so it is not trying to add the 2 new tables and relationships that I need. It is coming up blank. I have tried deleting the _migrationhistory table from the database and starting over but still nothing. What am I missing?

0

2 Answers 2

2

It sounds like you are adding the second migration without making any changes. What you need to do is this:

  • Add DBSet<ModelName> properties to your context for all existing tables.
  • Create the initial migration using -ignorechanges
  • Add DBSet<ModelName> properties to your context for all new tables.
  • Create the second migration as normal.

The second migration should then contain code to create only the new tables, relationships etc. you want. It doesn't matter whether you update the database in between migrations or only once at the end.

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

2 Comments

Ben Thank you that worked 90% of the way. However, now I am getting an error: Column 'dbo.Users.u_user_id' is not the same data type as referencing column 'PTEInteractiveCourses.ModifyUserId' in foreign key 'FK_dbo.PTEInteractiveCourses_dbo.Users_ModifyUserId'. Could not create constraint. In the database it is a numeric value. How do I fix this?
The error suggests you are trying to create a foreign key constraint between 2 columns that are not the same data type "numeric" is a touch vague, int, float, decimal?. Post the relevant code for your models and the table structure in a new question as this is new problem.
0

You could try to add normal migration and modify Up/Down methods so they include only the 2 new tables.

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.