0

I am maintaining a WPF application that use Entity Framework Code First with migrations (not auto). Some of the POCOs in this application are mapped to views (that point to another DB). I guess the method used to do this is similar to what is done in this answer: how to use views in code first entity framework

Now I want one of the POCOs (Company) to point to a table in stead of a view. The POCO has also changed a bit since pointing to the view.

When I add a migration after doing the changes in the POCO and in the CompanyConfiguration-class (changing the ToTable()), then it seems that the migration think that the view is an existing table and tries to rename it. For example the start of the Up()-method will look like this:

    RenameTable(name: "dbo.vCompany", newName: "Company");

    AlterColumn("dbo.Company", "ParentAccount", c => c.String(maxLength: 160));
    AlterColumn("dbo.Company", "Country", c => c.String(maxLength: 100));

However, I don't want the view to be changed, I want the Company-table to be created from scratch matching the POCO. What would be the correct/good way to accomplish this? Is it possible without writing the Up() and Down() methods myself?

Currently the application uses Entity Framework 6.1, but when this Company/vCompany mapping was first created, it was version 4.3 I belive.

Community
  • 1
  • 1
GHauan
  • 189
  • 1
  • 13

1 Answers1

1

Not much info/help to get on this, but I did the following that worked for me (although very tidious work):

I had to completely rewrite the auto-created Up() and Down() methods in the migration. Used the CreateTable() to create the table in stead of renaming the view.

In the Up() method:

CreateTable(
            "dbo.Company",
            c => new
            {
                CompanyId = c.Guid(nullable:false),
                ParentAccount = c.String(maxLength:160),
                Country = c.String(maxLength:100),
                Address1_City = c.String(maxLength:4000),
                Address1_Country = c.String(maxLength: 4000),
                Address1_Line1 = c.String(maxLength: 4000),
                Address1_Line2 = c.String(maxLength: 4000),
                Address1_PostalCode = c.String(maxLength:50),
                Owner = c.String(maxLength:160),
                OwnerId = c.Guid(nullable:false),
                Name = c.String(maxLength:160),
                EMailAddress = c.String(maxLength:100),
                InvoiceEMailAddress = c.String(maxLength:100),
                Fax = c.String(maxLength:50),
                CreditLimit = c.Decimal(storeType:"money"),
                CreditOnHold = c.Boolean(nullable:false, defaultValue:false),
                IsPrivate = c.Boolean(nullable:false, defaultValue:false),
                StatusCode = c.Int(),
                CustomerTypeCode = c.Int(),
                BEGreenCreditLimit = c.Int(),
                IssuingBodyAccount = c.String(maxLength:20),
                CustomerType = c.String(maxLength:100),
                IsElProducer = c.Boolean(nullable:false, defaultValue:false),
                IsEnergyTrader = c.Boolean(nullable:false,defaultValue:false),
                VATNumber = c.String(maxLength:100),
                ContactPerson = c.String(maxLength:160),
                ContactPersonPhone = c.String(maxLength:50),
                ContactPersonFax = c.String(maxLength:50),
                ContactPersonEmail = c.String(maxLength:100)

            }
            )
            .PrimaryKey(p=>p.CompanyId)
            ;

The company values extracted from another database, ref the long maxLength for some of the columns.

GHauan
  • 189
  • 1
  • 13