1

In my PersonModel, I removed a property which I no longer use. I also ensured that relationships are removed as well from the code. However, when I ran the application again, the application fails since the DbSet still refers to the old column. The query that's being generated is as follows:

{SELECT 
[Extent1].[StatusId] AS [StatusId], 
[Extent1].[Id] AS [Id], 
// some data 
[Extent1].[UserInformation_Id] AS [UserInformation_Id] // this is no longer in the model
FROM [dbo].[User] AS [Extent1]}

To investigate further, I created a migration script and I noticed that it generated a rename column line:

RenameColumn(table: "dbo.User", name: "UserInformationId", newName: "UserInformation_Id");

My assumption are as follows:

  • I have only changed the model. While I generated a Migration script, I think Add-Migration command base its migration script on the existing model, without double checking on what's on the database.
  • I might miss on other relationship mapping. But really, there are only 2 tables from which I am basing - User and UserInformation. User table doesn't have any foreign key from UserInformation. On the other hand, UserInformation maintains a many to 1 relationship with User (foreign key of UserId referring to the Id in User table).

The question is similar to this:

EF Code First adds extra column to query that doesn't exist in model anymore

Any help on where could possibly this issue went wrong?

Update

Here's my code:

User Class

public partial class User
{
    public long Id { get; set; }
    public Guid UserKey { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public virtual ICollection<UserInformation> UserInformations { get; set; }
}

User Mapping

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Username)
            .IsRequired()
            .HasMaxLength(255);

        this.Property(t => t.Password)
            .IsRequired();

        // Table & Column Mappings
        this.ToTable("User");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.UserKey).HasColumnName("UserKey");
        this.Property(t => t.Username).HasColumnName("Username");
        this.Property(t => t.Password).HasColumnName("Password");
    }
}

UserInformation Class

public partial class UserInformation
{      
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // This is the column that hold relationship to the User table. 
    public long UserId { get; set; }
    public virtual User User { get; set; }
}

UserInformation Mapping

public class UserInformationMap : EntityTypeConfiguration<UserInformation>
{
    public UserInformationMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.FirstName)
            .HasMaxLength(100);

        this.Property(t => t.LastName)
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("UserInformation");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.FirstName).HasColumnName("FirstName");
        this.Property(t => t.LastName).HasColumnName("LastName");
        this.Property(t => t.UserId).HasColumnName("UserId");

        // Relationships
        this.HasRequired(t => t.User)
            .WithMany(t => t.UserInformations)
            .HasForeignKey(d => d.UserId);
    }
}
Community
  • 1
  • 1
Patrick
  • 318
  • 3
  • 13

1 Answers1

0

To begin with, your first assumption is wrong. The migration generator will actually look at the tables in the database, mainly because it can't get a diff on the changes you made in your code. So it will generate the new model in memory and compare to whatever is in your DB.

Regarding the migration you saw, I suspect there is some remaining pushing EF to generate the foreign key (even if you can't access it in code). Maybe a navigation property like UserInformation => User? (just a guess, show us some code of the data model for a better answer)

pysco68
  • 1,136
  • 9
  • 15
  • the problem is similar to this [one](http://stackoverflow.com/questions/16149562/ef-code-first-adds-extra-column-to-query-that-doesnt-exist-in-model-anymore) – Patrick Mar 01 '15 at 13:52
  • As I said, I'd be happy to give you a more detailed answer, but I need some more info to fit the pieces together; show us some code of the models etc... – pysco68 Mar 01 '15 at 14:27
  • I updated my post to reflect the code implementation – Patrick Mar 01 '15 at 16:08
  • Okay, this way I don't see where the cliffhanger is, just two things I notice; you're using partial classes; any chance there's yet another partial hanging 'round? 2: In the second entity config you're specifying `.WithMany(t => t.UserInformations)` which shouldn't compile with the above code (no property `UserInformation` on entity `User`) – pysco68 Mar 01 '15 at 17:02
  • Yeah, I noticed that. I forgot to copy that portion. Actually, that still bugs me. Previously in the User class, there's a property named UserInformationId: public long UserInformationId { get; set; } This has been removed. The column UserInformationId is still in the DB. I would expect that that column will be removed since it is already removed from the model. Unfortunately, it didn't. Everytime I run the Users dbset in the query, it still includes the UserInformationId, although renamed to UserInformation_Id which is not what I want to happen. – Patrick Mar 02 '15 at 02:05
  • Have you tried clean / rebuild the full solution? Because if the code up there is really what you have in your solution some binary cruft is the sole explanation I can think of. – pysco68 Mar 02 '15 at 07:29