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);
}
}