0

I've got a class CurrentPage which has a Page property, and it's an enum (of type int):

namespace App.Model.Application
{
    using System.Runtime.Serialization;

    /// <summary>
    /// Represents user the current page of a user in the application
    /// </summary>
    [DataContract]
    public class CurrentPage
    {
        /// <summary>
        /// Gets or sets the unique identifier for this current page.
        /// </summary>
        [DataMember]
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the description 
        /// </summary>
        [DataMember]
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the identifier of the current page to which the user has navigated.
        /// </summary>
        [DataMember]
        public Page Page { get; set; }

        /// <summary>
        /// Gets or sets the current page as an integer; this is to support Entity Framework limitations regarding enumerations.
        /// </summary>
        public int PageId
        {
            get
            {
                return (int)this.Page;
            }

            set
            {
                this.Page = (Page)value;
            }
        }
    }
}

It's mapped like this:

this.Property(cp => cp.Page).IsRequired();

If I try to run Add-Migration in VisualStudio, I end up getting this in my migration code

RenameColumn(table: "dbo.CurrentPage", name: "Page1", newName: "Page");

I don't understand where this 'Page1' name is coming from. There's no Page1 column in the database.

Any ideas? Could it be creating the Page1 column because it somehow thinks the Page column that's already there isn't suitable for being mapped to that property?

Richard
  • 1,731
  • 2
  • 23
  • 54
  • What was the previous definition of the `CurrentPage` class (apparently from the migration the table is not new)? – Ivan Stoev Dec 19 '16 at 15:47
  • Could this be related? http://stackoverflow.com/questions/12363477/ef-code-first-how-to-prevent-duplicate-column-on-created-table-on-table-per-hi – Vlad274 Dec 19 '16 at 15:55
  • Regardless, could you please include the full definition of CurrentPage? This sort of naming thing is basically EF's way of saying "I need to represent the same thing twice" – Vlad274 Dec 19 '16 at 15:56
  • Yep sure, I've included it above. Note the last property PageId and the associated comments. I've inherited this code and it used to be using an older version of EF, but I've since updated it to EF6. I've since removed the PageId getter/setter but still see the same behaviour – Richard Dec 19 '16 at 15:59
  • Just trying to confirm that Add-Migration is actually looking at the same database as me! – Richard Dec 19 '16 at 16:03
  • It's hard to tell what the issue is, because the migration is based on the snapshot (classes, properties, mappings) of the last migration. Most likely the `PageId` was mapped to `Page` table column, and removing that mapping and adding another one messes up something. – Ivan Stoev Dec 19 '16 at 16:07
  • Yep I understand. I'm still digging around and will post an update if I get anywhere. Thanks for the tips! – Richard Dec 19 '16 at 16:36

1 Answers1

0

Not 100% sure what the cause of this was, but I deleted the PageId property and removed all existing migrations I had. Then I restarted Visual Studio (fwiw). The ran an update-database -script, and finally added a new migration (Add-Migration MyNewMigration) and it all started working again. Not a very satisfying answer I know, but it is working now. I also got a fresh copy of the database, untouched by my model changes before doing all that.

Richard
  • 1,731
  • 2
  • 23
  • 54