简体   繁体   English

实体框架中的模型关系

[英]Model relations in Entity Framework

I'm trying to build some models based on a database, i have two tables, one for workers and other is for branches. 我试图基于数据库构建一些模型,我有两个表,一个表用于工作人员,另一个表用于分支。 Every worker works in a branch, so every worker has only one branch and branches has unlimited workers. 每个工人都在分支机构工作,因此每个工人只有一个分支机构,而分支机构则有无限的工人。

My worker model: 我的工人模型:

public class Worker
{
    public Worker()
    {
        Overhours = new List<Overhour>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal WorkerId { get; set; }
    public decimal BranchId { get; set; }
    .....
    public virtual Branch Branch { get; set; }
    .....
}

Branches: 分行:

public class Branch
{
    public Branch()
    {
        Workers = new List<Worker>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal BranchId { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Bir şube adı girmelisiniz.")]
    public string BranchName { get; set; }
    public virtual ICollection<Worker> Workers { get; set; }
}

Here is the problem, i can access a workers branch using m.Workers.Find(Id).Branch but i want to access all workers working in a branch. 这是问题所在,我可以使用m.Workers.Find(Id).Branch访问worker分支,但是我想访问在分支中工作的所有worker。 So i want something like m.Branches.Find(Id).Workers . 所以我想要像m.Branches.Find(Id).Workers这样的东西。 It works but it seems weird to me. 它有效,但对我来说似乎很奇怪。 Because a worker object has already all branches in it also branch object has all workers too. 因为一个工作程序对象已经在其中所有分支,所以分支对象也有所有工作程序。 I don't even know i need this because i can easily get all workers who's working in a branch by using LINQ to Entity or raw SQL. 我什至不知道我需要这个,因为我可以通过使用LINQ to Entity或raw SQL轻松获取所有在分支中工作的工人。

What's the right way to do it? 什么是正确的方法?

The navigation properties ( Branch in Worker class and Workers in Branch ) are lazy loaded. 导航属性( Worker类中的BranchBranch中的Workers )是延迟加载的。 When you fetch a Worker from the database, it does not include the branch in it. 当您从数据库中获取一个Worker时,它不包含分支。 But whenever you need the branch (that is, when you read w.Branch ) the lazy loading mechanism kicks in and fetches it from the database. 但是,每当需要分支时(即,当您读取w.Branch ),惰性加载机制就会启动并从数据库中获取它。 This is also true for Workers in Branch . Branch Workers也是如此。

So as a simple answer, you're doing it the right way. 因此,作为一个简单的答案,您正在使用正确的方法。

BTW, there are lots of things to learn here: 顺便说一句,这里有很多东西要学习:

  • you can use eager loading to fetch everything you need in a single call to the DB 您可以使用紧急加载在一次对数据库的调用中获取所需的一切
  • you can turn off lazy loading, if you know you don't need related data it some parts of your code. 您可以关闭延迟加载,如果您知道不需要相关数据,则可以关闭代码的某些部分。
  • You can load some workers and then some branches and EF will connect them in case they are logically connected. 您可以加载一些工作程序,然后某些分支和EF将在逻辑连接的情况下将它们连接起来。 In some scenarios this might be faster than using lazy loading. 在某些情况下,这可能比使用延迟加载更快。

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

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