简体   繁体   English

一对多的外键太多

[英]Too Many Foreign Keys in a One-to-Many

This problem is similar to: 此问题类似于:

Code First Multiple Foreign Keys for One to Many Relation 为一对多关系编写第一个多个外键代码

and

Entity Framework Code First - two Foreign Keys from same table 实体框架代码优先-来自同一表的两个外键

But not similar enough for me to gleen a solution. 但是还不足够让我寻求解决方案。

I have a model "Team" that has one foreign key to another model called "Room" and a team can only have one room. 我有一个“团队”模型,其中一个外键指向另一个名为“房间”的模型,一个团队只能有一个房间。 The room model has two lists of Teams "p2teams" and "p3teams". 房间模型有两个团队“ p2teams”和“ p3teams”列表。 In the teams table it is actually creating three different id columns to rooms: "RoomID", "Room_RoomID", and "Room_RoomID1". 实际上,在teams表中,它为房间创建了三个不同的id列:“ RoomID”,“ Room_RoomID”和“ Room_RoomID1”。

This result in me trying to set a team's room (such as exampleTeam.Room = exampleRoom) and it only affects the RoomID column and not the rest. 这导致我尝试设置团队的房间(例如exampleTeam.Room = exampleRoom),并且只影响RoomID列,而不影响其余部分。 Furthermore, I just really don't fully get what is going on to have this result. 此外,我只是真的不完全了解发生这种情况的结果。

Below are my models, DB diagram, and some sample data from the team's table. 以下是我的模型,数据库图以及团队表中的一些示例数据。 Thanks for the help! 谢谢您的帮助!

Team Model: 团队模式:

public class Team
{
    public int TeamID { get; set; }

    public int HighschoolID { get; set; }
    public virtual Highschool Highschool { get; set; }
    public int ContestID { get; set; }
    public virtual Contest Contest { get; set; }

    [ForeignKey("Room")]
    public int? RoomID { get; set; }
    public virtual Room Room { get; set; }

    public string Warning { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

Room Model: 房间型号:

public class Room
{
    public int RoomID { get; set; }

    public virtual ICollection<RoomUsage> RoomUsages { get; set; }

    public string Name { get; set; }

    public int? AvailibleSeats { get; set; }
    public int? TotalSeats { get; set; }

    public virtual ICollection<Student> p1Students { get; set; }
    public virtual ICollection<Team> p2Teams { get; set; }
    public virtual ICollection<Team> p3Teams { get; set; }
}

Relevant DB Diagram Image 相关数据库图图像

First Few Rows of Teams Table 球队前几行表格

Once again, thanks for all your help. 再次感谢您的所有帮助。

Entity Framework is behaving properly. 实体框架行为正常。 If you set a Teams Room property you are only affecting the RoomID in that table. 如果设置“团队室”属性,则只会影响该表中的RoomID The other columns are changed in the Rooms Table. 其他列在“房间”表中更改。

Your Rooms Table should only have one ICollection<Team> . 您的房间表应该只有一个ICollection<Team> If you want a Room to have multiple lists of teams add a TeamTypeID to the Teams table. 如果您希望一个房间有多个团队列表, TeamTypeID向“团队”表中添加一个TeamTypeID A room can then have as many team lists as there are TeamTypes. 这样,一个会议室可以拥有与TeamTypes一样多的团队列表。

Because you have multiple ICollection<Team> in the Rooms table it is generating additional Columns in the Teams table. 因为您在“房间”表中有多个ICollection<Team> ,所以正在“团队”表中生成其他列。

Based on a lot of assumptions this is what I believe those two classes should look like 基于很多假设,我认为这两个类应该看起来像

Team: 球队:

public class Team
{

    public int TeamID { get; set; }

    public int? RoomID { get; set; }

    public string Warning { get; set; }

    public virtual Room Room { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public virtual ICollection<Contest> Contests { get; set; }
}

Room: 房间:

public class Room
{
    public int RoomID { get; set; }

    public string Name { get; set; }

    public int? AvailableSeats { get; set; }

    public int? TotalSeats { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public virtual ICollection<Team> Teams { get; set; }

    public virtual ICollection<Usage> Usages { get; set; }
}

I have created a script that creates database tables with I believe will achieve your goals based on even more Assumptions: 我已经创建了一个脚本来创建数据库表,并相信该脚本将基于更多的假设来实现您的目标:

• A student has
    One: Team, HighSchool, Room 
• A team has 
    One: Room
    Many: Contests 
• A room has 
    Many: Teams, Students, Usages 
• A contest has 
    Many: Teams 

If you still want to use code first you can use Entity Framework to generate Code First classes from these tables 如果仍然要先使用代码,则可以使用Entity Framework从这些表中生成Code First类。

create table dbo.HighSchools
(
    HighSchoolID int primary key,
    Name varchar(50)

)

create table dbo.Rooms
(
    RoomID int primary key,
    Name varchar(50),
    AvailableSeats int,
    TotalSeats int
)

create table dbo.Usages
(
    UsageID int primary key,
    Name varchar(50)
)

--If a Usage is Unique to each room I would remove this table and add RoomID to the Usages Table
create table dbo.RoomUsages
(
    UsageID int foreign key (UsageID) references Usages(UsageID),
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    primary key (UsageID, RoomID)
)


create table dbo.TeamTypes
(
    TeamTypeID int primary key,
    Name varchar(50)
)

create table dbo.Teams
(
    TeamID int primary key,
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    TeamTypeID int foreign key (TeamTypeID) references TeamTypes(TeamTypeID),
    Warning varchar(50)
)

--If a Student's room will always be the same as their Team's room I would remove the RoomID column from either Teams or Students 
create table dbo.Students
(
    StudentID int primary key,
    HighSchoolID int foreign key (HighSchoolID) references HighSchools(HighSchoolID),
    TeamID int foreign key (TeamID) references Teams(TeamID),
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    Name varchar(50),
    DOB date,

)

create table dbo.Contests
(
    ContestID int primary key,
    Name varchar(50)
)

create table dbo.ContestTeams
(
    ContestID int foreign key (ContestID) references Contests(ContestID),
    TeamID int foreign key (TeamID) references Teams(TeamID),
    primary key (ContestID, TeamID)
)

DB Diagram 数据库图

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

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