简体   繁体   English

包含DbSet的DbSet组

[英]DbSet Groups containing DbSets

I'm currently using Entity Framework for an application i'm creating, but I'm quite fresh, so some things keep confusing me. 我目前正在将Entity Framework用于正在创建的应用程序,但是我很新鲜,所以有些事情使我感到困惑。

I have a simple question which I couldn't find an answer for. 我有一个简单的问题,找不到答案。

Is it possible to store DbSets inside a DbSet? 是否可以在DbSet中存储DbSet? Say for example I want my users to be in groups. 举例来说,我希望我的用户分组。 Each group should have their own sets of Articles, Customers, etc. 每个小组应有自己的一套文章,客户等。

What I'm thinking is something like this: 我在想的是这样的:

public class Context : DbContext
{
    public DbSet<Group> Groups { get; set; }
}

public class Group: DbContext
{
    public List<User> Members { get; set; }

    public DbSet<Article> Articles { get; set; }
    public DbSet<Customer> Customers { get; set; }
}

I would then call for the articles like so: 然后,我将呼吁像这样的文章:

context.Groups.Find(GetGroupIdOfLoggedInUser()).Articles.Find(id);

Is this possible, and if so, is it a good solution? 这可能吗?如果可以,这是否是一个好的解决方案? Is there a better approach? 有没有更好的方法?

Regards, Robin 问候,罗宾

First: Think of DbSet as your SQL table. 首先:将DbSet视为您的SQL表。 So, you can not put a table inside a table. 因此,您不能在桌子内放桌子。

Second: DavidG is perfectly right. 第二:DavidG是完全正确的。 You need to do the following: 您需要执行以下操作:

public class Context : DbContext
{
    public DbSet<Group> Groups { get; set; }
}

public class Group
{
    public List<User> Members { get; set; }
    public List<Article> Articles { get; set; }
    public List<Customer> Customers { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM