MVC 和实体框架中有限选择的数据库设计?

发布于 2024-10-20 06:29:04 字数 487 浏览 1 评论 0原文

我真的不知道该在这个问题的标题中添加什么内容,但事情是这样的:

如果我希望能够有一个表顾问,其中包含用户及其个人信息,并且我希望该用户能够要选择他们对多个程序(软件)的熟练程度,范围为 0-5(零表示没有任何经验),我该如何做到最好?

我的意思是,我可以有一个与程序表具有一对多关系的顾问表,然后通过 XML 文件或其他文件加载所有可用的有限程序,并且对于每个顾问,将所有程序及其熟练程度关联起来。但这似乎是非常错误且低效的。

在我看来,我应该有一个包含所有(有限数量)程序的表,然后通过 ID 在两者之间建立某种关联。但我不知道如何做到这一点。我正在考虑多对多......但首先,这是正确的吗?其次,如何在实体框架模型中执行此操作?我通常首先创建数据库代码,即创建 EF 模型,然后从模型生成数据库。我会得到一个可以在其中添加“级别”字段的联结表吗?因为基本上顾问有一个具有级别的程序,或者实际上是每个都有一个级别的程序列表。

我对数据库太缺乏经验,无法弄清楚这一点。一步一步的解释将不胜感激!

I didn't really know what to put in the heading to this question, but here's the thing:

If I want to be able to have a table Consultant, which is the user and their personal information, and I want this user to be able to select their proficiency in a number of Programs (software) on a scale from 0-5 (zero being no experience whatsoever), how do I do this best?

I mean I could have a Consultant table with a one-to-many relationship to a Program table, and then have all the limited Programs availalbe loaded by an XML file or something, and for each Consultant associate all the Programs and their Levels of proficiency. But that seems very wrong and inefficient.

It seems to me I should have a table with all the (limited number of) Programs, and then by some sort of association between the two by Ids. But I can't get my head around how to do this. I'm thinking many-to-many... But first, is this correct? Secondly, how do I do this in an Entity Framework Model? I usually create my database code first, i.e. create the EF model, and then generate database from model. Will I get a junction table where I can add the Level field, because basically a Consultant HAS A Program with a Level, or actually a list of Programs each with a Level.

I'm too inexperienced with databases to figure this out. A step by step explanation would be greatly appreciated!

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

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

发布评论

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

评论(1

美羊羊 2024-10-27 06:29:04

创建三个表:

  • Consultant
  • Program
  • ConsultantProficiency

ConsultantProficiency 将具有以下列:

  • ConsultantId - FK 到顾问
  • ProgramId - FK 到程序
  • 级别

将 ConsultantId 和 ProgramId 标记为表的 PK。

将这些表添加到实体模型 - 它将创建三个具有所有关系和导航属性的实体。您还可以首先在 EDMX 中执行此操作,然后让 EF 为您生成 DB。

这里的要点是您无法隐藏联结表,因为您想将 Level 设置为附加属性。仅当不需要向联结表添加任何额外数据时,才可以直接表达多对多(隐藏联结表)。

Create three tables:

  • Consultant
  • Program
  • ConsultantProficiency

ConsultantProficiency will have these columns:

  • ConsultantId - FK to Consultant
  • ProgramId - FK to Program
  • Level

Mark ConsultantId and ProgramId as PK of table.

Add these tables to entity model - it will create three entities with all relations and navigation properties. You can also start by doing this in EDMX and let EF generate DB for you.

The point here is that you can't hide junction table because you want to set Level as additional property. Many-to-many can be expressed directly (hide junction table) only if you don't need to add any additional data to junction table.

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