簡體   English   中英

實體框架4(Linq查詢不會返回我的角色)

[英]Entity Framework 4 (Linq query doesn't return my roles)

我目前正在使用Entity Framework 4.0和POCO開發自己的會員資格版本。

在准備好Scotts Gu的博客文章之后,我決定盡可能多地使用約定。

到目前為止,我有一個名為User的類:

public class User
{
    [System.ComponentModel.DataAnnotations.Key]
    public int UserId { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(60)] 
    public string UserName { get; set; }

    public string Password { get; set; }

    [System.ComponentModel.DataAnnotations.ConcurrencyCheck]
    public string Email { get; set; }

    [System.ComponentModel.DataAnnotations.Timestamp]
    public byte[] Timestamp { get; set; }

    public ICollection<Role> Roles { get; set; }
}

還有一個叫做Role的類

public class Role
{
    [System.ComponentModel.DataAnnotations.Key]
    public int RoleId { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(50)]
    public string RoleName { get; set; }

    [System.ComponentModel.DataAnnotations.StringLength(300)]
    public string RoleDescription { get; set; }

    public ICollection<User> Users { get; set; }
}

我也像這樣實現DbContext:

public class BackboneDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
}

然后根據Scott的建議,我創建了從RecreateDatabaseIfModelChanges繼承的Initializer類。

然后在該類中,添加一些虛擬項目,如下所示:

    protected override void Seed(BackboneDbContext context)
    {
        var roles = new List<Role>
        {
            new Role
                {
                    RoleId = 0,
                    RoleName = "User",
                    RoleDescription = "This role belong to normal users.",
                }

        };

        roles.ForEach(r => context.Roles.Add(r));

        var users = new List<User>
        {
            new User  {UserId = 1, 
                       UserName = "Elham", 
                       Email = "abc@yahoo.com", 
                       Password = "xyz",
                       Roles = new List<Role>
                                   {
                                      roles[0]
                                   }

                       }

        };

        users.ForEach(u => context.Users.Add(u));
    }

我希望到這里為止都是有意義的。 如果不是這樣,則基本上,如果數據庫模式發生更改,以上代碼將使用偽數據填充表。

到目前為止,一切都很棒。 我得到以下表格:

角色,角色_用戶和用戶

他們都有我需要的信息,但是問題是,當我在Repository類中使用以下LINQ查詢來獲取所有USers時:

    public IQueryable<User> GetAllUsers()
    {
        IQueryable<User> allUsers = from u in _backboneDbContext.Users select u;

        return allUsers;
    }

現在,如果我在將allUsers傳遞給View之前對其進行了檢查,則會獲得用戶,但Role設置為'Null'

我不知道為什么...有任何想法嗎? 謝謝。

您必須添加[System.ComponentModel.DataAnnotations.Association]屬性。 例如:

public class User
{
    // [...]

    [System.ComponentModel.DataAnnotations.Association("User_Roles", "UserId", "RoleId")]
    public ICollection<Role> Roles { get; set; }
}

public class Role
{
    // [...]

    [System.ComponentModel.DataAnnotations.Association("Role_Users", "RoleId", "UserId")]
    public ICollection<User> Users { get; set; }
}

如果關聯是雙向的,則必須在User.RolesRole.Users屬性上添加此屬性。

嘗試將您的ICollection屬性設為虛擬。 這允許EF延遲加載關系。 或者,研究在查詢上使用Include方法以熱切加載相關實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM