Database design for limited number of choices in M

2019-05-29 14:14发布

问题:

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!

回答1:

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.