0

由於某種原因,我的EF代碼優先模型不與db同步。我得到這個錯誤:ef代碼優先模型不與db同步

{"Invalid column name 'Type_Id1'."} 

該字段實際上被稱爲'Type_Id',所以我不知道從哪裏出現1。 我有一個名爲Type_Id的表列,我也在我的類型實體模型中添加了一個Type_Id。

你們有什麼想法,爲什麼我會得到這個錯誤信息,加上爲什麼我在名字的末尾得到1?

感謝,Laziale

UPDATE:

我的任務等級:

public class Task 
    { 
     public Task() 
     { 
      Language = 1; 
      Grades = new HashSet<Grade>(); 
      Categories = new HashSet<Category>(); 
      Subjects = new HashSet<Subject>(); 
      Rooms = new Collection<Room>(); 
      Tools = new Collection<Tool>(); 
     } 

     [Key] 
     public int Id { get; set; } 

     public string Description { get; set; } 

     public virtual TaskType Type { get; set; } 

     public string Rules { get; set; } 

     [Required] 
     [StringLength(200), MinLength(1)] 
     public string Name { get; set; } 

     public int PreperationTime { get; set; } 

     public int InstructionTime { get; set; } 

     public int TaskTime { get; set; } 

     public int Type_Id { get; set; } 

     public string VideoLink { get; set; } 

     [Required] 
     public int Language { get; set; } 

     public int? MinimumParticipants { get; set; } 

     public int? MaximumParticipants { get; set; } 

     public int? Rating { get; set; } 

     [Required] 
     public string CreatedBy { get; set; } 

     public virtual ICollection<Grade> Grades { get; set; } 

     public virtual ICollection<Category> Categories { get; set; } 

     public virtual ICollection<Subject> Subjects { get; set; } 

     public virtual ICollection<Room> Rooms { get; set; } 

     public virtual ICollection<Tool> Tools { get; set; } 
    } 

的DbContext類:

public ApplicationDbContext() : base("DefaultConnection", false) 
     { 
     } 

     public DbSet<Task> Tasks { get; set; } 
     public DbSet<TaskType> TaskTypes { get; set; } 


     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
+0

會爲你的模型,你添加一些代碼? – Bazinga

+0

@Bazinga - 沒有真正的模型,這些類擁有db結構中的屬性,並且我有dbcontext類和所有類的dbset。請讓我知道你是否需要任何特定的東西。 Thx幫助 – Laziale

+1

至少發佈您的POCO類的代碼,並在DbContext中發佈您的模型構建器。 – DevilSuichiro

回答

1

您需要添加您的導航屬性的FK屬性。 EF正在創建Type_Id1,因爲Type_Id已經存在(儘管通過約定它不能說明它是FK)。

[ForeignKey("Type_Id")] 
public virtual TaskType Type { get; set; } 

https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships

+0

它實際上是FK。我應該刪除公共int Type_Id {get;組; }我目前有?謝謝 – Laziale

+0

不,你離開那裏,然後告訴EF這是你的FK,所以它不會創建Type_Id1 –