EF Code First 相关实体上下文失败

发布于 2024-10-31 09:43:02 字数 879 浏览 5 评论 0原文

不知道如何正确命名。我有两个处于 m:n 关系的实体:成员和角色。

public class Role
{
    public int Id { get; set; }
    public string Title { get; set; }

    public ICollection<Member> MembersInRole { get; set; }
}

public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

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

我制作了一些种子数据:
http://i56.tinypic.com/2vjvj1w.png

并编写了测试:
问题是,

我的会员实体没有分配角色,即使我在上下文中创建了它们(正如您在图像中看到的那样)。我不知道出了什么问题。数据库中的表似乎没问题。我不确定上下文实例是否有问题。但应该没问题,因为我一直在处理种子数据。

Dunno how to name this properly. I have two entities in m:n relationship: Member and Role.

public class Role
{
    public int Id { get; set; }
    public string Title { get; set; }

    public ICollection<Member> MembersInRole { get; set; }
}

public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

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

I have made some seed data:
http://i56.tinypic.com/2vjvj1w.png

And wrote test:

The problem is, that my Member entity has no roles assigned, even when I created them in context (as you can see at images). I don't know what's wrong. Tables in database seems to be ok. I am not sure if there isn't something wrong with context instances. But it should be ok, becuase I am working with seed data all the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-11-07 09:43:02

您的 MembersRoleRoles 导航属性不是虚拟的,因此 EF 无法为延迟加载创建代理。因此,您必须明确要求 EF 加载您的导航属性。要么将您的属性标记为虚拟:

public class Role
{
    public int Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Member> MembersInRole { get; set; }
}

public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

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

或使用@Yakimych 方法。 EF 4.1 中的急切加载也可以使用 lambda 定义:

Member admin = db.Members.Include(m => m.Roles)
                         .FirstOrDefault(m => m.Name == "Admin");

Your MembersRole and Roles navigation properties are not virtual so EF can't crete proxy for lazy loading. Because of that you must explicitely ask EF to load your navigation properties. Either mark your properties as virtual:

public class Role
{
    public int Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Member> MembersInRole { get; set; }
}

public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

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

Or use @Yakimych approach. The eager loading in EF 4.1 can be also defined with lambdas:

Member admin = db.Members.Include(m => m.Roles)
                         .FirstOrDefault(m => m.Name == "Admin");
放我走吧 2024-11-07 09:43:02

尝试通过显式包含角色来预先加载角色:

Member admin = db.Members.Include("Roles").FirstOrDefault(m => m.Name == "Admin");

Try to eager-load the roles by including them explicitly:

Member admin = db.Members.Include("Roles").FirstOrDefault(m => m.Name == "Admin");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文