ALTER TABLE ALTER COLUMN failed because column 'ProjectID' does not exist
in table 'User'
I am currently working on Code First Approach in Ef and I am getting a
strange error as 'ALTER TABLE ALTER COLUMN failed because column
'ProjectID' does not exist in table 'User'.' Here the projectID column
does exist in the User entity but still EF throws this error when I try to
run the following command in Powershell, Update-Database
Here are my project and User Entities,
public partial class User
{
public User()
{
LoggedByUserList = new List<Defect>();
ModifiedByUserList = new List<Defect>();
AssignedToUserList = new List<Defect>();
}
public int UserID { get; set; }
public string GPN { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
public Nullable<int> RoleID { get; set; }
public Nullable<int> ProjectID { get; set; }
public virtual Role Role { get; set; }
public virtual Project Project { get; set; }
public ICollection<Defect> LoggedByUserList { get; set; }
public ICollection<Defect> ModifiedByUserList { get; set; }
public ICollection<Defect> AssignedToUserList { get; set; }
}
public UserMap()
{
this.HasKey(u=>u.GPN);
this.ToTable("User");
this.Property(u => u.UserID)
.IsRequired()
.HasColumnName("UserID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(u=>u.GPN)
.IsRequired()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName("GPN")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.FirstName)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("FirstName")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.LastName)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("LastName")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.MiddleName)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("MiddleName")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.Designation)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("Designation")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.Email)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("Email")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.RoleID)
.HasColumnName("RoleID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(u => u.ProjectID)
.HasColumnName("ProjectID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasRequired(p => p.Project).WithMany(p =>
p.UserList).HasForeignKey(p => p.ProjectID);
this.HasRequired(r=>r.Role).WithMany(r=>r.UserList).HasForeignKey(r=>r.RoleID);
this.HasRequired(u => u.Project).WithRequiredDependent(u =>
u.ProjectManager);
}
public partial class Project
{
public Project()
{
ApplicationModuleList = new List<ApplicationModule>();
DefectList = new List<Defect>();
UserList = new List<User>();
}
public int ID { get; set; }
public string ProjectName { get; set; }
public User ProjectManager { get; set; }
public Nullable<System.DateTime> ProjectStartDate { get; set; }
public Nullable<System.DateTime> ProjectEstimatedEndDate { get; set; }
public Nullable<System.DateTime> ProjectActualEndDate { get; set; }
public Nullable<int> ProjectBillingModel { get; set; }
public Nullable<decimal> ProjectEstimatedBudget { get; set; }
public Nullable<decimal> ProjectActualBudget { get; set; }
public Nullable<int> ProjectPortfolio { get; set; }
public Nullable<decimal> ProjectBillingRate { get; set; }
public Nullable<int> ProjectEstimatedManHours { get; set; }
public Nullable<int> ProjectActualManHours { get; set; }
public Nullable<int> ProjectIsApproved { get; set; }
public string TestColumn { get; set; }
public virtual ICollection<ApplicationModule> ApplicationModuleList {
get; set; }
public virtual ICollection<Defect> DefectList { get; set; }
public virtual ICollection<User> UserList { get; set; }
public virtual BillingModel BillingModel { get; set; }
public virtual Portfolio Portfolio { get; set; }
public virtual ApprovalStatus ApprovalStatus { get; set; }
}
public ProjectMap()
{
this.HasKey(p => p.ID);
this.ToTable("Projects");
this.Property(p => p.ID)
.HasColumnName("ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
this.Property(p => p.ProjectName)
.HasColumnName("ProjectName")
.HasMaxLength(200)
.IsRequired()
.IsVariableLength()
.IsUnicode()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasOptional(p =>
p.BillingModel).WithMany(p=>p.Projects).HasForeignKey(p =>
p.ProjectBillingModel);
this.HasOptional(p =>
p.Portfolio).WithMany(p=>p.Projects).HasForeignKey(p =>
p.ProjectPortfolio);
this.HasOptional(p =>
p.ApprovalStatus).WithMany(p=>p.Projects).HasForeignKey(p =>
p.ProjectIsApproved);
}
I get the following error while running the Update-Database command,
Update-Database Specify the '-Verbose' flag to view the SQL statements
being applied to the target database. Applying code-based migrations:
[201308181758533_AddTestColumn]. Applying code-based migration:
201308181758533_AddTestColumn. System.Data.SqlClient.SqlException
(0x80131904): ALTER TABLE ALTER COLUMN failed because column 'ProjectID'
does not exist in table 'User'. at
System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection) at
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj) at
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,
Boolean async) at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) at
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction
transaction, MigrationStatement migrationStatement) at
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction
transaction, MigrationStatement migrationStatement) at
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1
migrationStatements) at
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1
migrationStatements) at
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String
migrationId, XDocument targetModel, IEnumerable1 operations, Boolean
downgrading, Boolean auto) at
System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration
migration, DbMigration lastMigration) at
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration
migration, DbMigration lastMigration) at
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1
pendingMigrations, String targetMigrationId, String lastMigrationId) at
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1
pendingMigrations, String targetMigrationId, String lastMigrationId) at
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String
targetMigration) at
System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ALTER TABLE ALTER COLUMN failed because column 'ProjectID' does not exist
in table 'User'.
Please help me in finding the cause of this error and how to resolve it.
Thanks in Advance
No comments:
Post a Comment