2013-02-04 49 views
0
public class User 
    { 
     [Key] 
     public int UserId { get; set; } 
     public string UserName { get; set; } 
    } 



public class Building 
    { 
     [Key] 
     public int BuildingId { get; set; } 
     public string BuildingName { get; set; } 
    } 

public class UserBuildings 
    { 
     //these are the foreign keys 
     public int UserId { get; set; } 
     public int BuildingId { get; set; } 
     public int BuildingQuantity { get; set; } 
    } 

我一直在尋找其他兩個forgein鍵的例子,但他們似乎並沒有回答我的問題。實體框架代碼優先:兩個外鍵表

在UserBuildings表中,UserId和BuildingId都需要使UserBuildings表中的記錄具有唯一性。 (不會有兩條記錄具有相同用戶名和BuildingId,儘管它們可能具有相同的其中之一)

回答

0

這應該這樣做

public class UserResources 
    { 
     [Key, Column(Order=0)] 
     public int UserId { get; set; } 
     [Key, Column(Order=1)] 
     public int BuildingId { get; set; } 
     public string BuildingName { get; set; } 
    } 
+0

感謝您的輸入。我已經嘗試過,但之後有一個SQL數據庫錯誤,說我不能有兩列具有相同值的列。 – Deniz

+0

澄清:錯誤是當我有兩個記錄具有相同的UserId但不同的BuildingId。但我不希望這給我一個錯誤 – Deniz

+0

我的不好,這完美的作品。謝謝!! – Deniz