English 中文(简体)
实体框架 4(Linq query don t 回去我的角色)
原标题:Entity Framework 4 (Linq query doesn t return my roles)

我目前正在使用实体框架4.0和POCO进行自我版本的成员编制工作。

在Scotts准备就绪后 Gu blog员额一决定尽可能使用公约。

到目前为止,我有一个称为用户的班子:

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; }
}

“作用”

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; }
}

随后,根据斯科特建议,我设立了从RereateDatabaseIfModelChanges继承的初始继承人。

之后,我增列一些my子,如:

    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));
    }

我希望这能成为现实。 如果不是的话,如果数据库进行图形改动,上述表格基本上将数据填满。

迄今为止,一切都绝对是巨大的。 我收到以下表格:

作用、作用和用户

他们都需要信息一,但问题在于我利用我的保存阶层的以下准则问询让所有美国人:

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

        return allUsers;
    }

现在,如果我先检查所有用户,然后再把他们转来观察,我就会接触到我的用户,但角色却被定在纽尔。

我不知道为什么...... 任何想法? 谢谢。

最佳回答

Try making your ICollection properties virtual. This allows EF to do lazy loading of the relationship. Alteratively, look into using the Include method on the query to eagerly load related entities.

问题回答

页: 1 系统.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; }
}

如果协会打算是双向的,就必须在上添加这一特性。 www.un.org/Depts/DGACM/index_french.htm





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签