15

我應該如何定義關係,但沒有使用任何導航屬性?EF4代碼優先:如何添加關係不添加使用代碼首先導航屬性

此前我已經定義一對多和多對多通過在關係的兩端使用導航屬性。並在數據庫中創建適當的關係。這裏有一個關於類的樣子的精簡版本(爲簡單起見,我將多關係轉換爲一對多關係)。

public class User 
{ 
    public string UserId { get; set; } 
    public string PasswordHash { get; set; } 
    public bool IsDisabled { get; set; } 
    public DateTime AccessExpiryDate { get; set; } 
    public bool MustChangePassword { get; set; } 

    public virtual Role Role { get; set; } 
} 

public class Role 
{ 
    public int RoleId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public virtual ICollection<User> Users { get; set; } 
    public virtual ICollection<Right> Rights { get; set; } 
} 

public class Right 
{ 
    public Guid RightId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public virtual Role Role { get; set; } 
} 

但是,如果我刪除導航屬性,則不會創建任何關係。這是類的樣子。

public class User 
{ 
    public string UserId { get; set; } 
    public string PasswordHash { get; set; } 
    public bool IsDisabled { get; set; } 
    public DateTime AccessExpiryDate { get; set; } 
    public bool MustChangePassword { get; set; } 

    public int Role RoleId { get; set; } 
} 

public class Role 
{ 
    public int RoleId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

public class Right 
{ 
    public Guid RightId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    public int RoleId { get; set; } 
} 

注意到,而不是導航屬性,我有相關表的主鍵。一切都在桌子上創造 - 除了關係。那麼我怎麼做到這一點呢?

順便說一句,我在的DbContext的OnModelCreating方法,但無濟於事嘗試各種組合。任何幫助深表感謝!

感謝, 梅爾

回答

24

我相信你使用的代碼時首先總是需要至少一個側面導航屬性。然後,您將能夠對其進行映射:

public class User 
{  
    public string UserId { get; set; }  
    public string PasswordHash { get; set; }  
    public bool IsDisabled { get; set; }  
    public DateTime AccessExpiryDate { get; set; }  
    public bool MustChangePassword { get; set; }  
    public int RoleId { get; set; } 
    public Role Role { get; set; } 
} 

public class Role 
{  
    public int RoleId { get; set; }  
    public string Name { get; set; }  
    public string Description { get; set; } 
} 

public class Right 
{  
    public Guid RightId { get; set; }  
    public string Name { get; set; }  
    public string Description { get; set; }  
    public int RoleId { get; set; } 
    public Role Role { get; set; } 
} 

public class TestContext : DbContext 
{ 
    public TestContext() : base("Entities") 
    {} 

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     modelBuilder.Entity<User>() 
      .HasRequired(r => r.Role) 
      .WithMany() 
      .HasForeignKey(r => r.RoleId); 

     modelBuilder.Entity<Right>() 
      .HasRequired(r => r.Role) 
      .WithMany() 
      .HasForeignKey(r => r.RoleId); 
    } 
} 
+0

我一直懷疑之多。感謝這個例子。我已經嘗試使用導航屬性以及外鍵,但外鍵未填充,但它看起來像您的示例中的答案。謝謝! – Mel 2011-03-09 02:57:05

+0

你需要定義ID屬性,如果屬性存在實際的對象?所以在User對象中,你需要定義RoleId還是僅僅爲了方便? – DDiVita 2011-04-11 20:01:21

+1

@DDiVita:這是一個很大的區別,如果你在'User'或不定義'RoleId'。我推薦這個答案看看有什麼不同:http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations/5282275#5282275 – 2011-04-11 20:07:25

0

在EF 4.3中,您可以添加一個添加關係的遷移。

+0

這個答案會受益於一個例子使用該功能。 – ryanyuyu 2015-10-07 16:31:16

1

您可以使用fluent api添加關係,但「配置」代碼與實體代碼是分開的,這使得它很難被發現。