简体   繁体   English

如何编写一对多的类C#

[英]How to write one to many classes c#

Here is my problem: I need to represent a one to many relationship. 这是我的问题:我需要代表一对多的关系。 One Company has many Stores. 一个公司有许多商店。 This is my code: 这是我的代码:

public class Company 
{
    public int Id;

    public string Name;

    public string Email;

    public Guid GUID;
}

public class Store 
{

    public int Id;

    public int CompanyId;

    public string Name;

    public int NoOfEmployees;

    public Guid GUID;
}

My question is: how this works without using virtual and hashsets? 我的问题是:如何在不使用虚拟集和哈希集的情况下工作? is this implementation ok? 这种实现可以吗?

Thank you. 谢谢。

You could use simply a List where you would save all the Stores that one company has. 您可以仅使用List来保存一个公司拥有的所有商店。

public class Company 
{
    public int Id;

    public string Name;

    public string Email;

    public Guid GUID;
    // List of all Stores
    public List<Store> AllTheStores;

    public Company()
    {
        AllTheStores = new List<Store>();
    }
}

EDIT: 编辑:

The property: public int CompanyId; 属性: public int CompanyId; makes only really sense if more than one Company can have the same Store . 只有一个以上的Company可以拥有同一家Store才真正有道理。 Otherwise you could access the stores via the Company: 否则,您可以通过公司访问商店:

Company c = new Company();

Store s = c.AllTheStores.Find(x=>x.Name == "MyStore");

// or 
List<Store> largeStores = c.AllTheStores.Where(x=>x.NoOfEmployees > 200).ToList();

As you are saying 正如你所说的

One Company has many Stores. 一个公司有许多商店。

In this statement your answer is. 在此声明中,您的答案是。

use Company class as follow, instead of putting companyID in Store class 按照以下方式使用Company类,而不是将companyID放在Store类中

public class Company 
{
    public int Id;
    public string Name;
    public string Email;
    public Guid GUID;

    // put a List of all Stores
    public List<Store> Stores;
}

So to answer the question how this works. 因此,回答这个问题是如何工作的。

Consider you have the id of the company you want to get all the stores for, you can simply do: 考虑您拥有要获取所有商店的公司的标识,您只需执行以下操作:

using (var dbcontext = new DbContext())
{
    var stores = await dbcontext.Stores.Where(x => x.CompanyId == companyId).ToListAsync();
}

You do not need navigation properties. 您不需要导航属性。 I never have them, they have only ever caused me issues, I'm not saying they will cause you issues. 我从来没有他们,他们只是曾经引起我的问​​题,我并不是说它们会导致您的问题。 They're just a convenience thing (from what it seems) which in my opinion obscures the underlying SQL representation and the queries EF could be running. 从我看来,它们只是一个方便的事情(从表面上看),它掩盖了基本的SQL表示形式以及EF可能正在运行的查询。 Now, there are going to be few queries which you actually care about EF generating, when you get to them, you may find that navigation properties are hindering you. 现在,您实际上关心的几乎是EF生成的查询,当您到达这些查询时,您可能会发现导航属性正在阻碍您。

If you want to save records, again, just assign each Store a real Company.Id and you will fulfill the foreign key obligations. 如果您想保存记录,再次给每个Store分配一个真实的Company.Id ,您将履行外键义务。

One change you could make (if this applies to your case) is that you can remove int Id and use the CompanyId as the primary and foreign key 您可以进行的一项更改(如果适用于您的情况)是您可以删除int Id并将CompanyId用作主键和外键

public class Store 
{

    // public int Id; just remove this

    public int CompanyId; // make this a foreign key using fluent or attributes.

    public string Name;

    public int NoOfEmployees;

    public Guid GUID;
}

This makes sure you retain the 1 to Many relationship, if those many should only exist for one company . 如果只有一个公司存在多个关系,这可以确保您保留“一对多”关系。 If not then don't worry about the above. 如果没有,那么不用担心以上问题。

You should use the company class object itself instead of just having CompanyId in the Store class. 您应该使用公司类对象本身,而不是仅在Store类中使用CompanyId。

public class Company 
{
    public int Id;

    public string Name;

    public string Email;

    public Guid GUID;

    public List<Store> Stores;
}

public class Store 
{

    public int Id;

    public Company Company; // Can get CompanyId using StoreObj.Company.Id

    public string Name;

    public int NoOfEmployees;

    public Guid GUID;
}

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

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