SQLite/C#:设计一个作为许多父实体的子实体的实体

发布于 2024-10-04 10:38:29 字数 960 浏览 0 评论 0原文

我有这些关系:

1 Period has N Documents
1 Pupil has N Documents
1 Report has N Documents

这些是我的 C# 实体:

class Period
{
   public int PeriodId {get;set;}   
   public List<Document> Documents {get;private set;}
}

class Pupil
{
   public int PupilId {get;set;}   
   public List<Document> Documents {get;private set;}
}

class Period
{
   public int ReportId {get;set;}  
   public List<Document> Documents {get;private set;} 

}

class Document
{
   public int DocumentId {get;set;}

   // ??? 
   public int PeriodId {get;set;}
   public int PupilId {get;set;}
   public int ReportId {get;set;} 

   // ??? OR just a general Foreign-Key Id like
   public int Id_FK {get;set;}
}

问题是这样的:

当我有一个带有 periodId 33 的文档时,我也有可能有一个 PupilId 33,因为两个表都使用主键/自动增量。因此,可能有一个学生和一个 ID 为 33 的句点。现在在两个表上执行 INNER JOIN,我将从我的 sqlite 查询中返回 2 个文档,尽管我应该只得到一个

你会如何解决这个问题?

I have these relations:

1 Period has N Documents
1 Pupil has N Documents
1 Report has N Documents

These are my C# entities:

class Period
{
   public int PeriodId {get;set;}   
   public List<Document> Documents {get;private set;}
}

class Pupil
{
   public int PupilId {get;set;}   
   public List<Document> Documents {get;private set;}
}

class Period
{
   public int ReportId {get;set;}  
   public List<Document> Documents {get;private set;} 

}

class Document
{
   public int DocumentId {get;set;}

   // ??? 
   public int PeriodId {get;set;}
   public int PupilId {get;set;}
   public int ReportId {get;set;} 

   // ??? OR just a general Foreign-Key Id like
   public int Id_FK {get;set;}
}

The problem is this:

When I have a Document with the PeriodId 33 there are chances that I could also have a PupilId 33 because both tables use primary key/autoincrement. So there could be a pupil AND a period having the Id 33. Doing now an INNER JOIN on both tables I would return 2 documents from my sqlite query although I should get ONLY ONE.

How would you solve that?

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

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

发布评论

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

评论(2

蓝颜夕 2024-10-11 10:38:29

首先,为 SQLite 定义 ERD,正确之后,就可以在 C# 中进行对象关系映射。您有四个主要实体,每个实体都需要 SQLite 中的一个表:Document、Pupil、Period、Report。然后,您需要一种方法来表达这三个表中每个表中的每个实体(行)都可以与一个或多个文档关联这一事实。正如马丁所说,这是使用“连接”(又名链接表)完成的。

现在,在 C# 中,如果您想要一个名为 Document 的非规范化对象,您可以这样做:

class Document
{
   public int DocumentId {get;set;}

   // ??? 
   public int PeriodId {get;set;}
   public int PupilId {get;set;}
   public int ReportId {get;set;} 

   // ??? OR just a general Foreign-Key Id like
  public int Id_FK {get;set;}
}

仅当一个文档可以与一个且仅一个 period、一个且仅一个 Pupil 以及一个且仅一个 Report 关联时。如果文档可以链接到多个实体,则 Document 类的这些属性必须允许多个值,即它们必须是数组或列表。

First, define your ERD for SQLite, and after that is correct, then you can do the object-relational mapping in C#. You have four main entities, each requiring a table in SQLite: Document, Pupil, Period, Report. And then you need a way to express the fact that each entity (row) in each of those three tables can be associated with one or more documents. As Martin said, that is done using a "junction" aka linking table.

Now, in C#, if you want to have a denormalized object called Document, you can do this:

class Document
{
   public int DocumentId {get;set;}

   // ??? 
   public int PeriodId {get;set;}
   public int PupilId {get;set;}
   public int ReportId {get;set;} 

   // ??? OR just a general Foreign-Key Id like
  public int Id_FK {get;set;}
}

only if a document can be associated with one, and only one Period, one and only one Pupil, and one and only one Report. If a document can be linked to more than one of those entities, these properties of your Document class would have to allow for multiple values, i.e. they would have to be arrays or lists.

黎夕旧梦 2024-10-11 10:38:29

您应该创建三个连接表(例如 period_docuemnts、ptudent_documents 和 report_documents)。这可能比必要的更笼统一些(单个文档也可能属于多个时期),但它更正常。

You should create three junction tables (say, period_docuemnts, pupil_documents, and report_documents). This may be a bit more general than necessary (a single document could belong to multiple periods with that, also), but its more normal.

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