2016-12-01 51 views
0

我啓用的Code First遷移,但一段時間後,我改變了模特屬性,所以當我跑給出以下錯誤的應用程序。改變模式後的Code First遷移

無效的列名等等......因爲遷移後,我改變了模型。

到目前爲止,我明白的問題是,我更新了模型,但這些變化並不適用於數據庫中的表...好心幫我解決it..means模特屬性和數據庫列不匹配

遷移已啓用。 我將自動遷移設置爲true,但我無法工作。

Error

public class Floor 
{ 
    public int FloorID { get; set; } 
    public string Name { get; set; } 

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


public class Room 
{ 
    public int RoomID { get; set; } 
    public string Name { get; set; } 

    [Display(Name ="Room Rent")] 
    public decimal Rent { get; set; } 
    [Display(Name="Floor")] 
    public int FloorID { get; set; } 

    public int Seater { get; set; } 

    [Display(Name="Attach Bathroom")] 
    public Boolean AttachedBathRoom { get; set; } 

    public virtual Floor Floor { get; set; } 
    public virtual ICollection<Student> Students { get; set; } 
} 

public class Student 
{ 

    public int StudentID { get; set; } 
    public string Name { get; set; } 
    public int CNIC { get; set; } 
    public int Phone { get; set; } 
    [Display(Name="Floor")] 
    public int FloorID { get; set; } 
    [Display(Name ="Room No")] 
    public int RoomID { get; set; } 

    public string Address { get; set; } 

    public string Email { get; set; } 
    public string Department { get; set; } 

    public virtual Floor Floor { get; set; } 
    public virtual Room Room { get; set; } 
} 

回答

0

首先,你需要在你的類指定主鍵和外鍵。

要指定一個主鍵,你的關鍵屬性之前添加[Key]。對於外鍵,您還必須添加鏈接的表。所以,在你的房間類,你就會有這樣的事情:

[Key] 
[ForeignKey("Floor")] 
public int FloorID { get; set; } 

做在所有車型所有的按鍵相同。

然後,你需要添加一個新的遷移,然後更新您的數據庫。

在你的包管理器控制檯,鍵入此添加一個新的遷移:

Update-Database 
+0

我執行你給出的命令後出現以下錯誤「:

Add-Migration FooBar 

然後使用此命令更新數據庫爲表'Rooms'指定多個標識列,每個表只允許一個標識列「。在包管理器中。 –

+0

如果我理解正確的話,你在房間FloorId是一個外鍵,對不對? –

+0

是FloorID是foriegn鍵。 –