将实体框架与SQL Server多对多关系使用

发布于 2025-02-08 12:41:37 字数 212 浏览 1 评论 0原文

不知道如何在此处使用“显示事物”的高级功能,因此请原谅;-)

数据库结构

User --> UserOwnerR <-- Owner

也是我有几个支持结构(例如,属于特定所有者的地址)。

我需要找到特定用户可以访问的所有地址,因为它属于/许多所有者,但是不是地址,用户与谁具有所有者关系。

Do not know how to use the advanced features of displaying thing here, so please excuse ;-)

The database structure is

User --> UserOwnerR <-- Owner

Also I have several support structures (ex. addresses belonging to a specific owner).

I need to find all addresses to whom a specific user has access because it belongs to on/many owners, but not addresses to whom the user have a owner relation.

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

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

发布评论

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

评论(2

虫児飞 2025-02-15 12:41:38

N:M关系可以在没有EF Core 5+中的联接表的情况下实现。

public class User
{
    // user properties 
    public IEnumerable<Owner> Owners { get; set; }

}

public class Owner
{
    // owner properties
    public IEnumerable<User> Users { get; set; }
}

n:m relations can be realized without a join table in EF Core 5+.

public class User
{
    // user properties 
    public IEnumerable<Owner> Owners { get; set; }

}

public class Owner
{
    // owner properties
    public IEnumerable<User> Users { get; set; }
}
煮酒 2025-02-15 12:41:38

您没有指定您使用EF代码的第一种方法(基于C#类生成架构)或数据库First方法(从数据库表中生成C#类)或无(手动设置您的实体)。

如果您能够手动更改课程,则可以添加导航属性。这些可能看起来像这样:

public class User
{
    // whatever 
    public IEnumerable<UserOwnerR> userOwners { get; set; }

}

public class Owner
{
    // whatever
    public IEnumerable<UserOwnerR> userOwners { get; set; }

}

public class UserOwnerR
{
    public virtual Owner owner { get; set; }
    public virtual User user { get; set; }
}

现在您可以在将这些桌子连接在一起的同时放置条件。将基于SQL语法的查询选项与LINQ一起使用,因为以这种方式连接表更容易。您可能想看看

You did not specify wether you’re using ef Code first approach (you generated your Schema based on c# classes) or database first approach (generate c# classes from database tables) or none of those (manually set up your entities).

If you are able to change your classes manually, you might add navigation properties. Those might look like this:

public class User
{
    // whatever 
    public IEnumerable<UserOwnerR> userOwners { get; set; }

}

public class Owner
{
    // whatever
    public IEnumerable<UserOwnerR> userOwners { get; set; }

}

public class UserOwnerR
{
    public virtual Owner owner { get; set; }
    public virtual User user { get; set; }
}

Now you are able to place conditions while joining those tables together. Use the sql syntax based query option with linq, as it’s easier to connect your tables that way. You might want to take a look at Entity Framework Join 3 Tables to construct your individual query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文