Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架4(Linq查询不返回我的角色)_C#_Entity Framework - Fatal编程技术网

C# 实体框架4(Linq查询不返回我的角色)

C# 实体框架4(Linq查询不返回我的角色),c#,entity-framework,C#,Entity Framework,我目前正在使用EntityFramework4.0和POCO开发自己的会员资格版本 在准备了Scotts Gu的博客后,我决定尽可能多地使用约定 到目前为止,我有一个名为User的类: public class User { [System.ComponentModel.DataAnnotations.Key] public int UserId { get; set; } [System.ComponentModel.DataAnnotations.StringLen

我目前正在使用EntityFramework4.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; }
}
公共类用户
{
[System.ComponentModel.DataAnnotations.Key]
public int UserId{get;set;}
[System.ComponentModel.DataAnnotations.StringLength(60)]
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
[System.ComponentModel.DataAnnotations.ConcurrencyCheck]
公共字符串电子邮件{get;set;}
[System.ComponentModel.DataAnnotations.Timestamp]
公共字节[]时间戳{get;set;}
公共ICollection角色{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; }
}
公共类角色
{
[System.ComponentModel.DataAnnotations.Key]
public int RoleId{get;set;}
[System.ComponentModel.DataAnnotations.StringLength(50)]
公共字符串RoleName{get;set;}
[System.ComponentModel.DataAnnotations.StringLength(300)]
公共字符串角色描述{get;set;}
公共ICollection用户{get;set;}
}
我还实现了DbContext,如下所示:

public class BackboneDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
}
公共类BackboneDbContext:DbContext
{
公共数据库集用户{get;set;}
公共数据库集角色{get;set;}
}
然后,根据Scott的建议,我创建了一个从RecreateDatabaseIfModelChanges继承的初始值设定项类

然后在该类中,我添加了一些虚拟项,如下所示:

    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));
    }
protected override void Seed(BackboneDbContext上下文)
{
var角色=新列表
{
新角色
{
RoleId=0,
RoleName=“用户”,
RoleDescription=“此角色属于普通用户。”,
}
};
roles.ForEach(r=>context.roles.Add(r));
var users=新列表
{
新用户{UserId=1,
UserName=“Elham”,
电子邮件=”abc@yahoo.com", 
Password=“xyz”,
角色=新列表
{
角色[0]
}
}
};
users.ForEach(u=>context.users.Add(u));
}
我希望到这里能有意义。如果不是,基本上,如果数据库模式发生更改,上述代码将使用虚拟数据填充表

到目前为止,一切都非常好。我得到以下表格:

角色、角色、用户和用户

它们都有我需要的信息,但问题是当我在我的存储库类中使用以下LINQ查询来获取所有用户时:

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

        return allUsers;
    }
public IQueryable GetAllUsers()
{
IQueryable allUsers=来自_backboneDbContext中的u。用户选择u;
回归诱惑;
}
现在,如果我在将allUsers传递给View之前检查它,我将获得我的用户,但角色设置为“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; }
}
公共类用户
{
// [...]
[System.ComponentModel.DataAnnotations.Association(“用户角色”、“用户ID”、“角色ID”)]
公共ICollection角色{get;set;}
}
公共阶级角色
{
// [...]
[System.ComponentModel.DataAnnotations.Association(“角色用户”、“角色ID”、“用户ID”)]
公共ICollection用户{get;set;}
}

如果关联是双向的,则必须在
用户.Roles
角色.Users
属性上添加此属性。

必须添加
[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; }
}
公共类用户
{
// [...]
[System.ComponentModel.DataAnnotations.Association(“用户角色”、“用户ID”、“角色ID”)]
公共ICollection角色{get;set;}
}
公共阶级角色
{
// [...]
[System.ComponentModel.DataAnnotations.Association(“角色用户”、“角色ID”、“用户ID”)]
公共ICollection用户{get;set;}
}

如果关联是双向的,则必须在
用户.Roles
角色.Users
属性上添加此属性。

尝试将ICollection属性设置为虚拟属性。这允许EF延迟加载关系。或者,研究在查询上使用Include方法来急切地加载相关实体

尝试将ICollection属性设置为虚拟。这允许EF延迟加载关系。或者,研究在查询上使用Include方法来急切地加载相关实体

谢谢你的回答,Timwi,我已经这样做了,但不幸的是它仍然不起作用!当我添加这些测试数据时,我可以看到关联工作,因为在表Roles\u Users中我获得了关联。但是当我像以前一样阅读它时,我的Role属性为null!您提到,对于Users属性,我应该添加关联属性,并且您使用“Role\u Users”作为第一个参数。这是表的名称还是仅仅是一个名称。因为我在我的表列表中没有看到该表,所以我看到的唯一关联表是:Roles\u Users?我相信它只是一个名称。在MicrosoftSQLServer中,表之间的外键约束将获得此名称。Timwi,我按照Wubbsy的建议将属性设置为虚拟属性,现在可以使用了。谢谢你的回答!:)谢谢你的回答,Timwi,我已经这样做了,但不幸的是它仍然不起作用!当我添加这些测试数据时,我可以看到关联工作,因为在表Roles\u Users中我获得了关联。但是当我像以前一样阅读它时,我的Role属性为null!您提到,对于Users属性,我应该添加关联属性,并且您使用“Role\u Users”作为第一个参数。这是表的名称还是表的名称