EntityFramework when columnname changed error is Invalid column name

asp.net-mvc-4 entity-framework entity-framework-6

Question

I am building an MVC4 application and using Entityframework. I am using code first model.

My Project class is as follows:

public class Project
    {
     [Key]
       public int ProjectId { get; set; }
       public ProjectContact ApprovalContact { get; set; }

        public ProjectContact BillContact { get; set; }

        public ProjectContact ProjectContact { get; set; }
     }

My ProjectContact class is as follows:

public class ProjectContact
    {
        [Key]
        public int ProjectContactId { get; set; }
        public string JobTitle { get; set; }
        public string Number { get; set; }
        public string ProjectContactMSFNo { get; set; }
        public string FullName { get; set; }

    }

While using Code first in EntityFramework database generated is

SELECT [ProjectId]
      ,[ApprovalContact_ProjectId]
      ,[BillContact_ProjectId]
      ,[ProjectContact_ProjectId]
  FROM [Db].[dbo].[Projects] 

Till now it is working fine

Now When I changed column [ApprovalContact_ProjectId] to [ApprovalContact] and try to insert project in database i get error: Invalid column name 'ApprovalContact_ProjectContactId'.

when i try to map model name in modelBuilder i get ERROR:Models.ProjectContact must be non nullable

How to tell DbContext to map [ApprovalContact_ProjectId] to [ApprovalContact]??

Accepted Answer

Have you tried following annotation

[ForeignKey("ApprovalContactID")]
public ProjectContact ApprovalContact { get; set; }

I have found this at : Entity Framework Code First : how to annotate a foreign key for a "Default" value?

and I also suggest you to look at : https://entityframework.codeplex.com/workitem/1226




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why